0

对 AS3 来说还是新手 - 我已经对此感到困惑了一段时间,无法弄清楚为什么它不会显示加载的 SWF(它加载它我可以通过跟踪看到,但是 onCompleteHandler 中的 addChild 什么都不做。要么我没有加载的 childSWF 的句柄,否则我完全迷路了。没有显示错误消息,但跟踪确实显示“this”不是舞台或任何可显示的东西,它是 Util 类实例。

我想要做的是将外部 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);
        }   

    }
4

1 回答 1

0

您正在做的是将Child 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
        }   

    }
}

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

于 2013-09-02T22:42:34.573 回答