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.