0

I'm trying to add an object to the stage. In the document class, I use the following code:

public function StartGame()
{
    gameClass = new GameClass(this);
    this.addChild(gameClass);
}

In the game, I'd like to add an object to the bottom of the stage. After that, it should move up untill it's outside of the screen. I've added the following code:

public function GameClass(main:MainClass) {
        this.main = main;
        viruslist = new Array();

        this.addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event):void {
        addVirus(75);
    }

    public function addVirus(xcoor)
    {
        trace("Creating virus");
        var v:Virus = new Virus(this.main, this, xcoor);
        this.addChild(v);
        viruslist.push(v);
    }

Then, in the Virus-class, I do the following:

public function Virus(main:MainClass, gameKlasse:GameClass, x:Number) {
        this.main = main;
        this.game = gameKlasse;
        this.xcoor = x;
        this.addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event):void {
        this.y = main.height;
        trace("Height: " + y);
        trace("Stage height: " + main.height);
        this.addEventListener(Event.ENTER_FRAME, runTime);
        trace("Virus created");
    }

So the problem is, my stage is about the height of my screen. But, the Virus gets placed almost to the top. My log shows:

Creating virus Height: 86 Stage height: 156.5 Virus created Height: 156.5 Stage height: 227 Virus created

What is going wrong? Why does it get created twice? Why does the stage height change? And why doesn't my object show up at the bottom of my screen?

EDIT: using stage.stageHeight gives the same results.

Joetjah
  • 6,292
  • 8
  • 55
  • 90

1 Answers1

1

You are using main.height and stage.height, while main and stage doesn't have any height until you add something to it. So it only gets a height bigger than zero once you add a virus.

What you're looking for is the stageHeight property which will tell you the available height. You can acces it in any object that is added to the stage by:

stage.stageHeight

good luck!

Michiel
  • 4,160
  • 3
  • 30
  • 42
  • Ok, let me edit my question since the problem persisted. Before I created the virus, I created the player object (on x=2 and y=2). So something was already added to the stage. I now logged main.height instead (because stage.stageHeight gave the same problem) but the values are exactly the same. – Joetjah Mar 17 '13 at 19:56
  • I understand your virus is created twice, are you sure that GameClass is only initiated once? And are you sure StartGame is only called once? – Michiel Mar 17 '13 at 20:08
  • I can't really see where the heights come from, are these classes related to graphic objects in you library? – Michiel Mar 17 '13 at 20:11
  • Yes they are, I've loaded them as Symbols. I'm using a MenuClass similar like this, and the stageheight and width are correctly loaded there. And I'm pretty sure everything is only created once. That's why I'm so confused about this... – Joetjah Mar 17 '13 at 20:16
  • Wait. I messed up. I used stage.height instead of stage.stageHeight. Your answer proved correct. I'm still wondering what the changing height values must be though, but the real problem is solved for now. Thank you! – Joetjah Mar 17 '13 at 20:22