加载外部swf时正确使用addtoStage

时间:2013-09-17 07:48:14

标签: actionscript-3 stack-overflow

我正在将外部swf文件加载到父swf文件中。

以前,我收到错误1009并通过使用侦听器事件在运行任何脚本之前将swf添加到舞台来修复它。

然而,当嵌入到父URL中时,swf无法完全加载,如此URL中所示 http://viewer.zmags.com/publication/06b68a69?viewType=pubPreview#/06b68a69/1

这是我正在使用的代码。

感谢您的任何意见。

package 
{
    import com.greensock.TweenLite;
    import flash.display.DisplayObject;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.SpreadMethod;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    import flash.utils.getDefinitionByName;

    public class slider5 extends Sprite
    {

        public var thumbPath:String = "Trailchair_thumb";
        public var featPath:String = "Trailchair";

        public var sliderIndex:Number = 1;
        public var currBg:Bitmap = new Bitmap();

        public var thumbCount:Number = 8;
        public var thumbHolder:Sprite = new Sprite();

        public var thumbMask:Sprite = new Sprite();

        public var thumbX:Number = 0;
        public var thmPadding:Number = 10;
        public var thmWidth:Number;

        public var navLeft:Sprite = new Sprite();
        public var navRight:Sprite = new Sprite();

        public var timer:Timer = new Timer(5000,0);
        public var sliderDir:String = "fwd";


        public function slider5()
        {
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        public function onAddedToStage(e:Event):void{
        removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

            //THE BACKGROUND IMAGE
            currBg.alpha = 1;
            stage.addChildAt(currBg, 0);
            changeBg(sliderIndex);


            //The thumbMask a sprite with graphic rectangle
            thumbMask.x = 87;
            thumbMask.y = 572;
            thumbMask.graphics.beginFill(0xFFFFFF);
            thumbMask.graphics.drawRect(0,0, 406, 181);
            stage.addChildAt(thumbMask, 2);


            //The thumbSlider
            thumbHolder.x = 228;
            thumbHolder.y = 573;
            stage.addChildAt(thumbHolder, 1);
            thumbHolder.mask = thumbMask;

            buildThumbs();

            //add the nav
            navLeft.x = 100;
            navLeft.y = 609;

            navRight.x = 496;
            navRight.y = 609;

            stage.addChildAt(navLeft, 4);
            stage.addChildAt(navRight, 4);

            var navBmp:Bitmap = new Bitmap();
            navBmp.bitmapData = new navarrow(109,109);

            var navBmp_Rt:Bitmap = new Bitmap();
            navBmp_Rt.bitmapData = new navarrow(109,109);

            navLeft.addChild(navBmp);
            navLeft.scaleX *=  -1;

            navRight.addChild(navBmp_Rt);

            navLeft.useHandCursor = true;
            navLeft.buttonMode = true;
            navRight.useHandCursor = true;
            navRight.buttonMode = true;

            navLeft.name = "left";
            navRight.name = "right";

            navLeft.addEventListener(MouseEvent.CLICK, navClick);
            navRight.addEventListener(MouseEvent.CLICK, navClick);


            //add the active item frame
            var frame:Sprite = new Sprite();
            frame.x = 226;
            frame.y = 570;
            frame.graphics.lineStyle(10, 0x000000);
            frame.graphics.drawRect(0,0,131, 181);

            stage.addChildAt(frame, 6);

            timer.addEventListener(TimerEvent.TIMER, timeEvt);
            timer.start();

        }


        public function changeBg(index):void
        {
            //set the first slide from our library and add to the stage
            var currBg_Class:Class = getDefinitionByName( featPath + index ) as Class;
            currBg.bitmapData = new currBg_Class(597,842);

            //fade it in
            TweenLite.from(currBg, 0.5, {alpha:0});
        }

        public function buildThumbs():void
        {
            var currThm:Class;

            for (var i:uint = 1; i<=thumbCount; i++)
            {
                currThm = getDefinitionByName( thumbPath + i ) as Class;
                var thmBmp:Bitmap = new Bitmap();
                thmBmp.bitmapData = new currThm(126,176);
                thmBmp.x = thumbX;
                thumbHolder.addChild(thmBmp);

                thumbX +=  thmBmp.width + thmPadding;
            }

            thmWidth = thmBmp.width + thmPadding;
        }


        public function navClick(e):void
        {

            timer.reset();
            timer.start();
            var dir:String = e.currentTarget.name;

            if (dir=="left" && thumbHolder.x < 228 )
            {
                sliderIndex--;
                TweenLite.to(thumbHolder, 0.5, {x:thumbHolder.x + thmWidth});
                //thumbHolder.x = thumbHolder.x + thmWidth;
            }
            else if (dir=="right" && thumbHolder.x > - 724 )
            {
                sliderIndex++;
                TweenLite.to(thumbHolder, 0.5, {x:thumbHolder.x - thmWidth});
                //thumbHolder.x = thumbHolder.x - thmWidth;
            }

            if (sliderIndex == thumbCount)
            {
                sliderDir = "bk";
            }

            if (sliderIndex == 1)
            {
                sliderDir = "fwd";
            }

            changeBg(sliderIndex);
        }


        public function timeEvt(e):void
        {
            if (sliderDir == "fwd")
            {
                navRight.dispatchEvent(new Event(MouseEvent.CLICK));
            }
            else if (sliderDir == "bk")
            {
                navLeft.dispatchEvent(new Event(MouseEvent.CLICK));
            }
        }



    }
}

1 个答案:

答案 0 :(得分:0)

如果您仍然需要它,您可以尝试这两个建议。注意我不知道Zmags并且最初认为它是您自己的域名。这就是我建议你使用Loader类的原因。当我做了一个parent.swf'的测试版本,它加载了包含你的代码的测试'child.swf'时,它对我有用。它实际上加载了孩子SWF没有问题。

  1. 从扩展Sprite更改为扩展MovieClip

  2. 避免检查是否已添加到此项目中的阶段

  3. 说明:

    1. 扩展MovieClip而不是Sprite
    2. I Long story short Flash不喜欢你的swf扩展Sprite然后由扩展Movieclip的父加载器打开。 ZMag播放器将扩展MovieClip。这是合乎逻辑的,文档确实在某种程度上证实了这一点。无论是否解决您的问题,只需在使用ZMags时将其保留为MovieClip。

      1. 在代码中避免舞台引用..
      2. 看看这个Zmags Q&amp; A documentaion:

        http://community.zmags.com/articles/Knowledgebase/Common-questions-on-flash-integration

        看问题4 ..在他们的回答中,这两个突出。

        • 上传的SWF中阶段参数的引用与出版物
        • 冲突
        • 您上传的无法找到的SWF中的错误或本地引用的资源

        是否真的有必要进行add_to_stage检查吗?如果它不会受到伤害,那么我建议从函数slider5()中删除stage_added检查,而是在函数onAddedToStage(e:Event)中剪切/粘贴你的代码。

        希望它有所帮助。