Don’t trust Stage.width

28 Feb07

Just found an issue with cached SWF files in IE7 - Stage.width will sometimes be reported incorrectly. We had a Movieclip that used Stage.width for positioning and whenever the SWF was cached, that movieclip would be positioned off stage. Hardcoding a variable instead of using the property solved the problem.

I seem to remember that there were some issues with Stage.width some years ago as well? Anyway - great to finally be doing some Flash work after editing JSP and portlets the last couple months…

28 February 2007 at 8:04 pm

5 Responses to Don’t trust Stage.width

  1. avatar photo
    Mike 28 February 2007 at 11:11 pm #

    If I recall correctly, you have to wait a frame for the Stage props to instantiate correctly. Putting all your code on frame 2 - or putting in a wait generally fixes these issues.

  2. avatar photo
    Josh 01 March 2007 at 8:32 am #

    Generally, you can get around weird stage size problems by listening for it’s resize event and doing your positioning in the event handler. At least one of the browsers out there doesn’t report the correct value when the SWF first starts playing.

  3. avatar photo
    Jensa 01 March 2007 at 4:51 pm #

    Hi Mike,
    Sounds probable, but unfortunatly it won’t work for me. I don’t use frames at all since I only do class based stuff using FlashDevelop and all my code is usually on frame one.

    I think Josh’s idea will work, since the resize event should trigger after the first frame is drawn. Thanks for good pointers!

    The odd thing is that if I trace the value to a text field in the faulty SWF, it will report the correct value despite failing to position the MC? Odd…

  4. avatar photo
    maliboo 01 March 2007 at 6:35 pm #

    Use interval instead of frames and wait couple ms for “initialization”. You can do it gracefully with setInterval and dispatching custom event (sceneInitialized, or sth). In event handler write your code for layouting.

  5. avatar photo
    Dmitry 08 October 2008 at 1:02 pm #

    import flash.display.Sprite;

    public class Main extends Sprite
      if (stage) init();
      else addEventListener(Event.ADDED_TO_STAGE, init);
      removeEventListener(Event.ADDED_TO_STAGE, init); 
      stage.addEventListener( “resize”, onWindowResize );