Reputation: 81
I'm noob and trying some ActionScript3 code and now I'm stuck with this problem.
I can't figure out why stage height changes after addEventListener.
Can someone help please?
I don't know is this code enough to find what's wrong with it. But here's the code:
...
[SWF(frameRate="60",width="768",height="1280",backgroundColor="#1c1c1c")]
...
private function loop():void {
trace("waiting for mouse");
trace("stage height before mouse click: ",stage.stageHeight);
//here stage height is 1024
stage.addEventListener(MouseEvent.CLICK, mouse);
}
private function mouse(event:MouseEvent):void {
trace("stage height after mouse click: ",stage.stageHeight);
//here, after addEventListener stage height is 800
var mouseX:Number = event.stageX;
var mouseY:Number = event.stageY;
stage.removeEventListener(MouseEvent.CLICK, loop);
trace("click: ", mouseX, ",", mouseY);
draw(mouseX, mouseY);
}
private function draw(mouseX:int,mouseY:int):void {
...
addChild(textField);
loop();
}
}
}
Upvotes: 4
Views: 300
Reputation: 911
This is probably an issue of air app. If you are using air there are several frames on the start where air sets up the correct screen size.
To handle that first set up this.
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
This will dispatch an resize event once the stage height changes.
So, before you do anything in your app you add listeners in this order.
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
stage.addEventListener(Event.Resize, onResize);
}
private function onResize(event:Event):void {
_resizeCounter ++;
trace("stage.stageWidth is "+ stage.stageWidth);
trace("stage.stageHeight is "+ stage.stageHeight);
if (resizeCounter == ${dependsOnThePlatform}) {
initializeTheApp();
}
}
Depending on the platform, you may need to listen for resize once or twice. Experiment with the stage.stageWidth
and stage.StageHeight
after the resize event
P.S. This all goes into the main App, and when you have everything set up you initialize Starling
Upvotes: 3