Copy link to clipboard
Copied
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();
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;
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Ok ...
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
That's excellent! You're welcome!
Do you want to save the index of the SWF loaded, the timestamp of each load...?
I think I didn't get what you need. Would you mind giving me more info?
Regards,
JC
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
OH MY GOD I CAN'T BELIEVE IT!!!
Dear JoãoCésar, I'm not a programmer and I don't know much about AS3, but you just did something that No one around the entire web , in many different forums I posted and searched Never could do ...With many people's suggestions and ideas to do this, no one could do the job ... Someday I posted a forum, "I'm thinking why showing multiple SWF files and remembering where you left is that hard in 2018?"
But now I see, it's not that hard if you know a REAL PROGRAMMER like you ... Adobe should be thankful for genius programmers like you ...
Copy link to clipboard
Copied
WOW! THANK YOU!
It's always amazing when I'm able to help someone.
Although I'm far away from being such a good programmer, I do what I can because I know what it feels when we need some answers and we don't find it.
The important thing here is that you found what you were looking for! ![]()
I wish you the best in your code adventure and have a nice week!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more