0
votes

I'm having an issue with playing an external SWFs inside the main SWF. Everything loads great, except not on cue. I'm using a simple delay actionscript to pause the main timeline until the external SWFs load, but it doesn't always sync up when testing in browsers.

Is there an AS3 code that I can use to pause the main timeline until the external SWF is finish loading and playing? I need to do this multiple times through the movie, btw...

Below is the delay and the loadmovie array I'm using.

<--------------//Timer//--------------->

var timer4:Timer = new Timer(1500, 1);
timer4.addEventListener(
  TimerEvent.TIMER,
  function(evt:TimerEvent):void {
    play();
  }
);
timer4.start();




<--------------//loadMovie//--------------->

function startLoad()
{
    var mLoader:Loader = new Loader();
    var mRequest:URLRequest = new URLRequest('flip/note_flip.swf');
    mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
    mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
    mLoader.load(mRequest);
}

function onCompleteHandler(loadEvent:Event)
{
        addChild(loadEvent.currentTarget.content);
}

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

startLoad();
1

1 Answers

0
votes

The load() method of the loader object is asynchronous. It means that the process will continue to execute next lines of code and loading is done independently. That's why you can see it's not on cue.

The simple way to fix your problem is to stop() all the loaded movies upon Event.COMPLETE and play() them once all the movies are loaded.

Use an array to store the loaded movies and address them once all movies are loaded.

And yeah, the timer hack is pointless, you won't know how long it will take the file to load, it can be different every time.

EDIT:

Let's assume that you've got N movie .swf files that makes a complete scene. You would like to load all of them, and once they're all loaded - play them.

Let's build a simple step-by-step logics:

1) First of all, we need a list of the URL pointing to where the external SWFs are.

2) Then we want to start loading the SWFs one by one (and not altogether) to prevent Flash Player bug of not loading some of the content AND keeping the order of movie clips aka z-index, depth, layer order etc.

3) On each loaded movie we count how many are still left to be loaded.

4) Once all movies are loaded, we play() all these movies.

Here's the code for you (written in a frame in case if you're not using classes and not familiar with OOP):

import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.MovieClip;

var linkArray:Array = ["1.swf", "2.swf", "3.swf"];
var loadedMovieArray:Array = [];
var totalMovies:int = linkArray.length;
var loadedMovies:int = 0;

var urlRequest:URLRequest = new URLRequest();
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onMovieLoaded);

loadMovies();

function loadMovies():void
{
    var movieIndex:int = loadedMovies;
    var movieURL:String = linkArray[movieIndex];

    urlRequest.url = movieURL;

    l.load(urlRequest);
}

function onMovieLoaded(e:Event):void
{
    var loadedMC:MovieClip = l.content as MovieClip;
    loadedMC.stop();

    loadedMovieArray.push( loadedMC );
    loadedMovies++;

    if (loadedMovies == totalMovies)
        onAllMoviesLoaded();
    else
        loadMovies();
}

function onAllMoviesLoaded():void
{
    for (var i:int = 0; i < loadedMovieArray.length; i++) {
        var mc:MovieClip = loadedMovieArray[i];
        addChild(mc);
        mc.play();
    }
}