flash插入页面后在IE下stageWidth为0的bug

用SWFObject,将swf文件嵌入网页后,在IE浏览器中首次浏览正常,刷新后,stageWidth就为0了,swf中基于stageWidth定位的元素,就会错乱。

 

转自(ljhzzyx 2009-09-24 14:44:20

 

 

首先区分一下stage的stageWidth和width属性。stage.stageWidth是flash player的宽度,stage.width是指stage里content的宽度,如果这个舞台(stage)里什么内容都没有,stage.width就是0。

        现在的需求是在运行期获得舞台的高宽,以期编码实现将组件添加到舞台中央等这样的效果。但是我得到的stageWidth 和 stageHeight 都为0。

        对于某个问题,老外的解释总是很详尽:http://hubflanger.com/stage-resize-and-the-stagewidth-and-stageheight-properties/

Turns out that when the stage is initialized, it's stageWidth and stageHeight properties are 0 until a Stage RESIZE event is triggered. This event is dispatched when the scaleMode property of the Stage object is set to StageScaleMode.NO_SCALE and the SWF file is resized ie. when the stage is rendered by the Flash Player. So if you need to add a display object onto the stage and its positioning is dependent on the stageWidth or stageHeight properties at runtime, it’s best handled by a listener that is subscribed to the Stage RESIZE event.
       在IE中SWF文件的stageWidth 和 stageHeight 被初始化为0,再延迟一段时间后会更新成正确的数值。在SWF文件更新stageWidth 和 stageHeight属性时,会触发stage对象的resize事件。因此, 可以给stage添加resize事件处理器,将取得stageWidth 和 stageHeight值及后续工作放在resize处理函数里做,一旦stageWidth 和 stageHeight值大于0,则将此resize监听器取消掉。

Interestingly, despite universal claim that the Flash Player behaves exactly the same on all browsers, this problem only appears in a couple of browser/plugin configurations, namely FireFox/OSX and IE/Win. And strangely, on Safari and Firefox/Win, the RESIZE event is not even dispatched on page load, and neither does the Flash Player register the stageWidth and stageHeight properties as being 0. I have not tested the problem on other platforms but you are certainly welcome to do so yourselves and report your observations in the comments section. 
       一般认为,flash文件在不同浏览器中的行为是一致的。如上所述,在FireFox/OSX 和 IE/Win这种搭配才有这样的bug。

在html页面使用使用SWFObject也有这样的问题:http://foxling.cn/as-flex-air/swfobject-bug-at-ie/。将这位同志的经验抄录如下:
       在as里解决的方法与上面所述是一致的。

SWFObject.js里的解决方案

在swfobject.js里找到函数function createSWF(attObj, parObj, id)里面针对winIE的处理方法是:
        el.outerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' + att + '>' + par + '</object>';
       看了一下Adobe自己的AC_OETags.js,他是用document.write方法直接写入,没有这个问题;
       直接写静态html(object)没有这种问题;
       直接访问这个flash的地址,也没有这种问题。

 

以下转自(http://www.woria.cn/blog/read.php/604.htm

SWFObject在IE下的BUG
如果Flash里绘制的对象的宽高是自适应Flash的宽高的,那么,使用SWFObject来插入Flash在IE会导致一个问题,当这个Flash被缓存后,也就是第二次访问该页面时,在该Swf文件被加载时,获取到的stage.stageWidth和stage.stageHeight为0,绘制的对象也就看不到了。


在Flash里监听resize事件,找出解决方法
用IE的话,会看到5行,有两次resize事件,这也就导致了Flash在加载的时候绘制对象错误(宽和高为0):
数字是stage.stageWidth和stage.stageHeight

info
resize
0 x 0
resize
300 x 300

如果用Firefox等浏览器,只输出两行文字,没有resize事件:

info
300 x 300

AS里的解决方案
通过监听resize事件,当stage.stageWidth和stage.stageHeight大于0时再进行初始化
package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;

    public class AutoSizeExample extends Sprite
    {
        private var txt:TextField;
        public function AutoSizeExample()
        {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;

            txt = new TextField();
            txt.multiline = true;
            txt.wordWrap = true;
            txt.text = "info/n";
            addChild(txt);

            if (stage.stageWidth>0 && stage.stageHeight>0){
                createChild();
            }else{
                stage.addEventListener(Event.RESIZE,onResize);
            }
        }

        private function onResize(e:Event):void
        {
            if (stage.stageWidth>0 && stage.stageHeight>0){
                stage.removeEventListener(Event.RESIZE,onResize); //删除事件监听
                createChild();
            }
            //否则继续监听事件,直到stage.stageWidth和stage.stageHeight大于0时才初始化
        }

        private function createChild():void
        {
            //进行初始化操作,创建各对象
            //.......................
            var w:Number = stage.stageWidth;
            var h:Number = stage.stageHeight;

            txt.appendText(w + " x " + h + "/n");
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值