Skip to main content
Known Participant
April 24, 2018
Answered

How To Loop Sequence of Loaded SWF Files?

  • April 24, 2018
  • 2 replies
  • 1677 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

So here is another Question ...

I've tried something NEW ... it's almost the whole day but no result yet...

Here is the Idea PLEASE HELP ...

In your code, How can I save or remember, last time which swf file is closed and next time you just start from that one... (It should remembers which swf is finished and next time you will start from that one instead of child1.swf )

For this I used SharedObject and tried to save number of swf file that is just loaded in a variable ,but I can't handle it in correct way...

Thanks

JoãoCésar17023019
Community Expert
Community Expert
May 7, 2018

With Special Thanks Dear JoãoCésar

I thought of something just like "Saving Game levels" ... each level is a separate swf file that should be load into Main swf ... And the Main swf should remembers which level (Which swf file) the user achieved yet ...By the way, your code was "Awesome" for this, because it loops and because it doesn't take any RAM more than RAM is needed for loaded swf (It deposes all swfs except for loaded one) , and "you can load dozens of swf files" without losing any Memory ...

Back to the problem, I think I should save "Index of the SWF loaded", then start loading next swf from that Saved Index...

To be more clear, let's say the sequence of swfs reaches "child2.swf" . Right now I close Main swf, then I open Main swf again and here instead of child1.swf I should face child2.swf...

I've tried SharedObject to save "++count % swfs.length" and do loading from saved  " ++count % swfs.length" But I just confused myself !!!

Any help is greatly appreciated

The code I used was something like this :

import flash.net.SharedObject;

var mySO:SharedObject = SharedObject.getLocal("somerandomname");

++count % swfs.length = mySO.data.my_vis;

mySO.data.my_vis = ++count % swfs.length;

mySO.flush ();

mySO.clear();


Sure!

What we can do is to store the current value of the 'count' variable everytime a SWF is loaded.

And everytime the app loads, we check to see if there is some value stored in the user's storage device.

Like this:

import com.greensock.*;

import com.greensock.loading.*;

import com.greensock.events.LoaderEvent;

import flash.display.DisplayObjectContainer;

import flash.events.Event;

import flash.net.SharedObject;

var sharedObject:SharedObject;

var swfIndexFile:String = "swf_index";

var swfIndexProp:String = "swfIndex";

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

{

    save(swfIndexProp, count);

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

    }

}

function onLoadSWFIndex(index:Object):void

{

    count = uint(index);

    loadSWF(count % swfs.length);

}

function noIndex():void

{

    loadSWF(count % swfs.length);

}

function load(file:String, dataProperty:String, loadCallback:Function, failCallback:Function):void 

    sharedObject = SharedObject.getLocal(file); 

 

    if(sharedObject.data[dataProperty]) 

        loadCallback(sharedObject.data[dataProperty]);

    else

        failCallback();

 

function save(dataProperty:String, value:Object):void 

{     

    sharedObject.data[dataProperty] = value;

    sharedObject.flush(); 

}

bar.scaleX = 0;

load(swfIndexFile, swfIndexProp, onLoadSWFIndex, noIndex);

I hope this helps.

Please don't hesitate to ask if you still have any further questions.

Regards,

JC

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

}