通过实用程序类加载外部AS3 SWF或Assets

时间:2013-09-02 21:44:56

标签: actionscript-3 flash

还是AS3的新手 - 我一直在讨论这个问题并且无法弄清楚为什么它不会显示加载的SWF(它加载它我可以看到通过跟踪,但onCompleteHandler中的addChild什么都不做要么我没有加载的childSWF的句柄,要么我完全迷失了。没有显示错误消息,但是跟踪确实显示“this”不是舞台或任何可显示的,它是Util类实例。 / p>

我想要做的是将外部swf加载到模板swf文件中,然后通过AS3操作加载的swf(假设它在我想要主要处理的所有内容上都有实例名称,如文本字段和标题和按钮)

package com.foo.flashas3
{
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.text.TextField;
    import fl.motion.MotionEvent;


    public class Main extends MovieClip
    {
        public var childSWF:MovieClip;
        public var swfURL:URLRequest  = new URLRequest("some.swf");
        public var util:Utils=new Utils();

        public function Main()
        {
            // constructor code

            trace("Entering Main");
            trace(this);
            initSplash();
        }


        public function initSplash():void
        {
            util.getSubClip(swfURL);
        }

    }

}

package com.foo.flashas3
{
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    import flash.text.TextField;

    public class Utils extends MovieClip
    {
        private var mLoader:Loader;
        public var mContent:MovieClip;
        public function Utils (){
            trace("Utils Constructor")
        }

        public function getSubClip(mRequest:URLRequest)
        {
            trace ("In getSubClip")
            mLoader = new Loader();
            mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
            mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
            mLoader.load(mRequest);
        }



        private function onProgressHandler(mProgress:ProgressEvent)
        {
            var percent:Number = mProgress.bytesLoaded / mProgress.bytesTotal;
            trace(percent);
        }

        private function onCompleteHandler(loadEvent:Event)
        {
            trace("In onCompleteHandler")
            mContent = MovieClip(mLoader.contentLoaderInfo.content);
            trace(mContent.totalFrames);
            trace(this)
            trace(this.parent)
            addChild(mContent);
        }   

    }

1 个答案:

答案 0 :(得分:0)

您正在做的是将addChild mContent添加到util对象。并且util不是addChilded到任何东西,因此不显示mContent。 有很多方法可以解决这个问题。 其中之一是将对要添加swf的对象的引用传递给:

package com.foo.flashas3
{
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.text.TextField;
    import fl.motion.MotionEvent;


    public class Main extends MovieClip
    {
        public var childSWF:MovieClip;
        public var swfURL:URLRequest  = new URLRequest("some.swf");
        public var util:Utils=new Utils();

        public function Main()
        {
            // constructor code

            trace("Entering Main");
            trace(this);
            initSplash();
        }


        public function initSplash():void
        {
            util.getSubClip(swfURL, this); // add reference to current object
        }

    }

}

package com.foo.flashas3
{
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    import flash.text.TextField;

    public class Utils extends MovieClip
    {
        private var mLoader:Loader;
        public var mContent:MovieClip;

        private var target:Sprite; // <- object to add loaded swf

        public function Utils (){
            trace("Utils Constructor")
        }

        public function getSubClip(mRequest:URLRequest, target:Sprite)
        {
            this.target = target; // <- store target reference
            trace ("In getSubClip")
            mLoader = new Loader();
            mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
            mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
            mLoader.load(mRequest);
        }



        private function onProgressHandler(mProgress:ProgressEvent)
        {
            var percent:Number = mProgress.bytesLoaded / mProgress.bytesTotal;
            trace(percent);
        }

        private function onCompleteHandler(loadEvent:Event)
        {
            trace("In onCompleteHandler")
            mContent = MovieClip(mLoader.contentLoaderInfo.content);
            trace(mContent.totalFrames);
            target.addChild(mContent); // <- add swf to target
        }   

    }
}

注意:我无法测试代码,但如果它没有运行,你会明白...