05-18-2009
Actionscript 3: issue with stage sizes
I’ve noticed a few issues with different browsers and not getting the stage.stageWidth property on load. To fix this I use an enterFrame to check when the stage.stageWidth property is greater than zero and the continue building the display list.
I hadn’t noticed this issue before, because I use a resize event for full flash sites or projects that expand with the browser size. I’ve done a few projects that don’t resize after the initial build, and that is where the issue lies.
Here is my process:
//-----------------------------------
// CONSTRUCTOR
//-----------------------------------
public function Main():void
{
addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
//-----------------------------------
// INIT
//-----------------------------------
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.showDefaultContextMenu = false;
stage.quality = StageQuality.BEST;
addEventListener(Event.ENTER_FRAME, checkStage, false, 0, true);
}
private function go():void
{
_bg = new Sprite();
_bg.graphics.beginFill(0x000000);
_bg.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
_bg.graphics.endFill();
addChild(_bg);
}
//-----------------------------------
// EVENTS
//-----------------------------------
private function checkStage(e:Event):void
{
if(stage.stageWidth > 0)
{
removeEventListener(Event.ENTER_FRAME, checkStage);
go();
}
}