简体   繁体   English

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

[英]Load external AS3 SWF or Assets via utility class

Still new to AS3 - I've been futzying with this for a while and can't figure out why it will not display the loaded SWF (it loads it I can see that via trace, but addChild in the onCompleteHandler does nothing. Either I don't have a handle to the loaded childSWF or I'm totally lost. No error msg is displayed, but the trace does show that "this" is not the stage or anything displayable, its the Util class instance. 对AS3来说仍然是新手-我一直对此感到疑惑,无法弄清楚为什么它不会显示已加载的SWF(加载它后,我可以通过跟踪看到它,但是onCompleteHandler中的addChild却什么也没做。没有加载的childSWF的句柄,或者我完全迷失了,不会显示任何错误消息,但跟踪结果确实表明“ this”不是阶段或任何可显示的东西,它是Util类实例。

What im trying to do is load an external swf onto a template swf file and then manipulate the loaded swf via AS3 (provided it has instance names on all the things I want to mainipulate like text fields and titles and buttons) 我想做的是将外部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);
        }   

    }

What you are doing is to addChild mContent to the util Object. 您正在执行的操作是将childmContent添加到util对象。 And the util is not addChilded to anything, so mContent is not displayed. 而且util不会添加到任何东西,因此不会显示mContent。 There are many ways you could fix this. 有很多方法可以解决此问题。 One of them is to pass a reference to the object you want to add the swf to : 其中之一是传递对要向其中添加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
        }

    }

}

and

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
        }   

    }
}

Note: I could not test the code, but if it do not run, you get the idea ... 注意:我无法测试代码,但是如果它不运行,您就会明白...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM