搜索

  • 好友链接

  • 链接表

  • Meta

    日志分类

    日志归档

    本月top点击

    历史top点击

    13

    stage.stageWidth和stage.stageHeight IE 中为0的bug

    我先按我理解的说下,下面有英文的原文
    bug:在IE中SWFs的stageWidth 和 stageHeight 被初始化为0,再延迟一段时间后会更新成正确的数值.
    解决方案:当swf更新stageWidth 和 stageHeight 的值时,会触发一个舞台的resize 事件,你可以指定一个回调,然后再开始处理显示对象!
    原文如下:
    There’s a strange bug in SWFs embedded in IE using SWFObject where even after your ADDED_TO_STAGE events have fired, on initialisation both stage.stageWidth and stage.stageHeight are 0. This can cause no end of problems in your app, particularly if you’re relying on these two values to measure your UI assets for layout.

    The solution isn’t a perfect one, but it works nonetheless….

    In IE the SWFs stageWidth and stageHeight are initialised at zero, then after a short delay they are updated to the correct values. There are a number of ways you could handle this delay (such as using a timer), but the only certain way of ensuring the values have truely updated before attempting to use them is to listen for the stage RESIZE event. When the SWF is updated with it’s correct width and height values (milliseconds after it’s initialised), Flash fires the resize event for the stage. You can then assign a callback that then kicks-off the sizing and positioning of the display objects.

    When the app reaches the point where it needs to get the stageWidth and stageHeight values, and finds them to be 0, you need to assign

    1. stage.addEventListener( Event.RESIZE, onStageResize )

    This will cause an the necessary event callback once IE ‘allows’ these values to be updated. When the callback is raised, we do a quick sanity check to ensure the stageWidth and stageHeight aren’t still zero, and then allow the app to continue with its measurements.

    BE CAREFUL though, as in Firefox and other browsers, the RESIZE event probably won’t be called, and the width and height will already be correctly set, so you’ll want to your app to call onStageResize( null ) straightaway. Remember also to remove the RESIZE event listener after you’ve has a satisfactory callback, so that you don’t keep repeating measuring, drawing and positioning routines.

    1. public function MyConstructor(){
    2.      addEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
    3. }
    4. private function onAddedToStage( e:Event ) :void{
    5.     if( stage.stageWidth == 0 && stage.stageHeight == 0 ){
    6.           stage.addEventListener( Event.RESIZE, onStageResize );
    7.       }else{
    8.             onStageResize( null );
    9.         }
    10.     }
    11. }
    12. private function onStageResize( e:Event) :void{
    13.        if( stage.stageWidth > 0 && stage.stageHeight > 0 ){
    14.               stage.removeEventListener( Event.RESIZE, onStageResize );
    15.                //Do stuff
    16.           }
    17. }

    相关日志

    1 条评论

    留下评论