Archive

Archive for March, 2009

stage scaleMode and stage size problems in Firefox

March 19th, 2009 admin 1 comment

Have you ever had the problem that, when opening the HTML that displays your Flash movie in the browser, all the stuff that is on stage i not positioned correctly?

I have. I encountered this problem with Firefox, when I noticed that objects were not centered on stage while this was the case when I ran the swf standalone or in Safari.

I managed to isolate the problem, and it turned out that what caused it was setting stage.scaleMode to StageScaleMode.NO_SCALE. After doing that, the stageWidth and stageHeight property values changed to zero.
It turned out that this could be solved by not implementing swfobject, but I didn’t want that, so I found a workaround in AS3.

To hack around this problem, I check the stageWidth and stageHeight values to see if either equals 0, and if so, I register a resize event listener, because I found that although the values become zero in Firefox, a resize event is also immediately dispatched after this. So setting a listener for this event and initializing the application inside the handler method solved my problem! Of course, you also want to trigger the handler manually in case there’s no bug, so I just call it directly if neither stageWidth or stageHeight equals 0.

Simple, huh? I know it’s a hack and I’d rather not need it, but at least my application behaves like I want it to now :)

Here’s the code:

public function Application()
{
    stage.align = StageAlign.TOP_LEFT;
    stage.scaleMode = StageScaleMode.NO_SCALE;
    if (stage.stageWidth == 0 || stage.stageHeight == 0)
    {
        stage.addEventListener(Event.RESIZE, handleStageResize);
    }
    else
    {
        handleStageResize();
    }
}

private function handleStageResize(event : Event = null) : void
{
    stage.removeEventListener(Event.RESIZE, handleStageResize);
    // initialize application here
}

Categories: programming Tags: