Skip to main content
Known Participant
April 24, 2018
Answered

How To Loop Sequence of Loaded SWF Files?

  • April 24, 2018
  • 2 replies
  • 1696 views

READY! Here it is : We have a simple but useful code that loads 2 swf files in sequence ...

But How can I Loop it?

How can you change the code to load swf1 after swf2 is finished?

I've tried almost the whole day but no result yet... Please help...Even with your comments any any any idea is greatly appreciated.

Thanks a lot...

Here is the code:

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();

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

Here is my suggestion.

Basically, I created a function to generate the SWF loader and I used an incremental value (count) to iterate in loop through an array that stores the dynamic infos of each loader. In this case, count % swfs.length will result in 0, 1, 0, 1... Effectively loading the SWFs in a loop sequence.

import com.greensock.*;

import com.greensock.loading.*;

import com.greensock.events.LoaderEvent;

import flash.display.DisplayObjectContainer;

import flash.events.Event;

var count:uint = 0;

var currentSWFLoader:SWFLoader;

var swf:Object = {};

var swfs:Array =

[

    {path:"child1.swf", container:this},

    {path:"child2.swf", container:this}

]

function generateSWFLoader(path:String, container:DisplayObjectContainer):SWFLoader

{

    return new SWFLoader

    (

        path,

        {

            container: container,

            y:         100,

            onProgress:progressHandler,

            onComplete:completeHandler,

            autoPlay:  false

        }

    );

}

function loadSWF(index:uint):void

{

    if (currentSWFLoader)

        currentSWFLoader.dispose(true);

   

    swf = swfs[index];

    currentSWFLoader = generateSWFLoader(swf.path, swf.container);

    currentSWFLoader.load();

}

function progressHandler(e:LoaderEvent):void

{

    bar.scaleX = e.target.progress;

}

function completeHandler(e:LoaderEvent):void

{

    e.target.rawContent.play();

    addEventListener(Event.ENTER_FRAME, checkFrame);

}

function checkFrame(e:Event):void

{

    if (currentSWFLoader.rawContent.currentFrame == currentSWFLoader.rawContent.totalFrames)

    {

        trace("swf done playing");

        removeEventListener(Event.ENTER_FRAME, checkFrame);

        loadSWF(++count % swfs.length);   

    }

}

bar.scaleX = 0;

loadSWF(count % swfs.length);

I hope it helps.

Regards,

JC

2 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
April 26, 2018

Hi.

Here is my suggestion.

Basically, I created a function to generate the SWF loader and I used an incremental value (count) to iterate in loop through an array that stores the dynamic infos of each loader. In this case, count % swfs.length will result in 0, 1, 0, 1... Effectively loading the SWFs in a loop sequence.

import com.greensock.*;

import com.greensock.loading.*;

import com.greensock.events.LoaderEvent;

import flash.display.DisplayObjectContainer;

import flash.events.Event;

var count:uint = 0;

var currentSWFLoader:SWFLoader;

var swf:Object = {};

var swfs:Array =

[

    {path:"child1.swf", container:this},

    {path:"child2.swf", container:this}

]

function generateSWFLoader(path:String, container:DisplayObjectContainer):SWFLoader

{

    return new SWFLoader

    (

        path,

        {

            container: container,

            y:         100,

            onProgress:progressHandler,

            onComplete:completeHandler,

            autoPlay:  false

        }

    );

}

function loadSWF(index:uint):void

{

    if (currentSWFLoader)

        currentSWFLoader.dispose(true);

   

    swf = swfs[index];

    currentSWFLoader = generateSWFLoader(swf.path, swf.container);

    currentSWFLoader.load();

}

function progressHandler(e:LoaderEvent):void

{

    bar.scaleX = e.target.progress;

}

function completeHandler(e:LoaderEvent):void

{

    e.target.rawContent.play();

    addEventListener(Event.ENTER_FRAME, checkFrame);

}

function checkFrame(e:Event):void

{

    if (currentSWFLoader.rawContent.currentFrame == currentSWFLoader.rawContent.totalFrames)

    {

        trace("swf done playing");

        removeEventListener(Event.ENTER_FRAME, checkFrame);

        loadSWF(++count % swfs.length);   

    }

}

bar.scaleX = 0;

loadSWF(count % swfs.length);

I hope it helps.

Regards,

JC

Known Participant
May 6, 2018

Ok ...

That works... Truly Amazing! Thank you for your help...

Ned Murphy
Legend
April 25, 2018

Couldn't you just do something along the lines of repeating the load for swf2 as part of an else condition in that very last conditional?

   if (currentSWFLoader == swf1) {
  currentSWFLoader
.dispose(true) // dispose and unload content
  currentSWFLoader
= swf2;
  currentSWFLoader
.load();
  
} else {

  currentSWFLoader.dispose(true) // dispose and unload content
  currentSWFLoader
= swf1;
  currentSWFLoader
.load();

}