如何循环加载的SWF文件的序列?

时间:2018-04-24 21:22:21

标签: loops flash load sequence

就绪! 这是:我们有一个简单但有用的代码,可以按顺序加载2个swf文件......

但我该如何循环呢?

如何在swf2完成后更改代码以加载swf1?

我已经尝试了几乎一整天,但还没有结果......请帮助...即使您的评论任何任何想法都非常感谢。

非常感谢...

以下是代码:

import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import flash.events.Event;


//create SWFLoaders


var swf1:SWFLoader = new SWFLoader("child1.swf",{container:this,y:100,onProgress:progressHandler,onComplete:completeHandler,autoPlay:false});
var swf2:SWFLoader = new SWFLoader("child2.swf",{container:this,y:100,onProgress:progressHandler,onComplete:completeHandler,autoPlay:false});
var currentSWFLoader:SWFLoader = swf1;


//adjust the progress bar;
function progressHandler(e:LoaderEvent):void {
    bar.scaleX = e.target.progress;
    trace(e.target.progress);
}


//tell the loaded swf to play and start tracking frames
function completeHandler(e:LoaderEvent):void {
    //the target of the LoaderEvent is the SWFLoader that fired the event
    //the rawContent is the loaded swf
    e.target.rawContent.play();
    addEventListener(Event.ENTER_FRAME, checkFrame);
}


function checkFrame(e:Event):void {
    //check to see if loaded swf is done playing
    if (currentSWFLoader.rawContent.currentFrame == currentSWFLoader.rawContent.totalFrames) {
        trace("swf done playing");
        removeEventListener(Event.ENTER_FRAME, checkFrame);


        //if the first swf is done playing load the second swf
        if (currentSWFLoader == swf1) {
            currentSWFLoader.dispose(true) // dispose and unload content
            currentSWFLoader = swf2;
            currentSWFLoader.load();
        }
    }
}


bar.scaleX = 0;
currentSWFLoader.load();

1 个答案:

答案 0 :(得分:0)

您需要 OOP 方法。

首先,创建一个完全处理一个外部SWF的类:加载,播放,表示播放结束,卸载和清理。

package
{
    // Imports.
    import flash.events.Event;

    import flash.net.URLRequest;

    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.display.MovieClip;

    public class ExternalMovie extends Sprite
    {
        private var L:Loader;
        private var M:MovieClip;

        // Handles loading.
        public function loadAndPlay(url:String):void
        {
            L = new Loader;
            addChild(L);

            L.contentLoaderInfo.addEventListener(Event.INIT, onLoaded);
            L.load(new URLRequest(url));
        }

        // Handles Loader.contentLoaderInfo event Event.INIT.
        private function onLoaded(e:Event):void
        {
            L.contentLoaderInfo.removeEventListener(Event.INIT, onLoaded);

            // Get a direct reference to the loaded movie root.
            M = L.content as MovieClip;

            // Set up the watchdog for loaded movie to finish.
            addEventListener(Event.ENTER_FRAME, onWatch);
        }

        // Event.ENTER_FRAME handler to watch the loaded movie playback.
        private function onWatch(e:Event):void
        {
            if (M.currentFrame >= M.totalFrames)
            {
                M.stop();

                // Announce the end of playback.
                dispatchEvent(new Event(Event.COMPLETE));
            }
        }

        // Unloads movie and cleans up.
        public function dispose():void
        {
            removeEventListener(Event.ENTER_FRAME, onWatch);

            L.unloadAndStop(true);

            removeChild(L);

            L = null;
            M = null;
        }
    }
}

其次,编写一个控制脚本,它创建该类的实例,告诉它通过其URL播放SWF,然后等待播放结束并继续播放下一部电影。

import ExternalMovie;

var EML:ExternalMovie;

var current:int = -1;
var plan:Array = ["child1.swf", "child2.swf"];

// Start the first one manually.
playNext();

function playNext(e:Event = null):void
{
    // Clean up the last one, if any.
    if (EML)
    {
        removeChild(EML);

        EML.removeEventListener(Event.COMPLETE, playNext);
        EML.dispose();
        EML = null;
    }

    // Switch to next url.
    current++;

    // Loop from the beginning if last movie just played.
    if (current >= plan.length)
    {
        current = 0;
    }

    // Get the next movie URL.
    var nextURL:String = plan[current];

    EML = new ExternalMovie;

    // Subscribe to Event.COMPLETE event to know the exact moment the
    // current movie finished playing to remove it and load the next one.
    EML.addEventListener(Event.COMPLETE, playNext);
    EML.loadAndPlay(nextURL);

    addChild(EML);
}

请注意,以上所有内容都是指导原则而不是完整的解决方案(尽管可能工作)。