Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
rojharris

Playing bunch of swfs one at a time with timer

Recommended Posts

Hi. This is driving me nuts. I can't find any working solutions anywhere on the net for what would seem to be an easy thing. Essentially I just want to play a bunch of existing swfs in sequence. Turns out that's almost impossible to do without errors all over the place. I figured I'd just give up and try loading and playing each swf for so many seconds then go on to the next, knowing that each one lasts about 8 seconds.

I figured that LoaderMax would be the way to go and after ploughing through other forum posts and doing some cut and paste here's my code. Now obviously it doesn't work but then I'm an illustrator not a programmer and I don't really understand any of this stuff beyond a very basic level. I cannot imagine how I'd get anything to work without this nice simple greensock stuff!!

Anyway, my plan was to load up an array of swfs, get the first playing in a timer function and then just cycle through them indefinitely. Trouble is I can't work out how to play the swfs once they are loaded. Where am I going wrong and is there a better way? (as obviously my stage.addChild is not working)

 

------------------------

LoaderMax.activate([sWFLoader]);

var queue:LoaderMax = LoaderMax.parse(["swfs/Kahuna.swf","swfs/Sirius.swf","swfs/kahuna360.swf","swfs/Morpheus.swf","swfs/IQModular.swf","swfs/ICE.swf","swfs/Alchemist.swf","swfs/Archangel.swf"],{maxConnections:1});
queue.load();

var fl_TimerInstance:Timer = new Timer(10000,0);
fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
fl_TimerInstance.start();

var count:Number = 1;
function fl_TimerHandler(event:TimerEvent):void
{
//play the swfs every 10 seconds
stage.addChild(queue[count]);
count++;

if (count >= 6)
{
	count = 0;
}

}

------------------------------

Link to comment
Share on other sites

I just saw Chris' post above about possibly using TimeLineMax as a way to play a sequence of swfs. That would be great, I could use delays within tweens to time them too. I'll bet it's too good to be true.

 

Or.... another thought: I could use TimeLineMax to append a delayed tween of an invisible movieclip every 10seconds, and have a new SWFLoader between each append, that loads and plays my swf.

Then somehow unload the lot and start again in an infinite loop....Hmmm.

Link to comment
Share on other sites

Playing swfs back-to-back is trickier than you might think for several reasons:

 

1) There's no "MovieClipTimelineFinish" event or anything like that which would be really helpful in being notified when the timeline finishes playing.

 

2) Depending on how they're built, swfs very often start executing code and perform actions as soon as the first frame is loaded, so it's not possible to just say "load this swf and make it not start until I tell it to..." Consequently, unless you have control over exactly how the swf is built and can make sure it won't start anything until it hits frame 2 or a particular method is called, it's best to just load the swf when you need it (no sooner).

 

3) You shouldn't use timers because it assumes the swf will play EXACTLY at the right speed, but if the CPU gets bogged down at all, the frame rate could drop which means the swf would take longer to finish so your timer won't coincide with the end time.

 

Technically you could use TimelineLite or TimelineMax by creating sequenced frame tweens of your various swfs, but that doesn't get around #2 from the above list. The cleanest way would probably be something like:

 

var urls:Array = ["swfs/Kahuna.swf","swfs/Sirius.swf","swfs/kahuna360.swf","swfs/Morpheus.swf","swfs/IQModular.swf","swfs/ICE.swf","swfs/Alchemist.swf","swfs/Archangel.swf"]; 

var curLoader:SWFLoader;
var curSWF:MovieClip;

playSWF(urls[0]);

function playSWF(url:String):void {
if (curLoader != null) {
	curSWF = null;
	curLoader.dispose(true);
	removeEventListener(Event.ENTER_FRAME, checkFrame);
}
curLoader = new SWFLoader(url, {container:this, onComplete:completeHandler});
curLoader.load();
}

function completeHandler(event:LoaderEvent):void {
curSWF = curLoader.rawContent;
addEventListener(Event.ENTER_FRAME, checkFrame, false, 0, true);
}

function checkFrame(event:Event):void {
if (curSWF.currentFrame == curSWF.totalFrames) {
	var index:int = urls.indexOf(curLoader.url) + 1;
	if (index 			playSWF(urls[index]);
	} else {
		trace("done with all swfs");
	}
}
}

Link to comment
Share on other sites

Ah, I see the problem now. I hadn't thought it through! So, As I do have access to all the swfs (I made them), could I put some kind of stop() on frame 1 then, in my host swf, tell it to gotoandPlay(2) of the child swf once it's loaded? Then move onto the next one once current frame == end frame kind of thing?

 

Thanks for the code, I'll try it out, then see if I can understand it. I am learning more about coding every day. Last time I wrote any programs was in the 70's or 80's on a Dragon32 computer! 32k of RAM wow! (and that was in assembly language!)

 

cheers

Roger

Link to comment
Share on other sites

Sure, you could put a stop() on frame 1 or just set autoPlay:false on the SWFLoader and then when you're ready, call play() on the swf (SWFLoader's rawContent). You could even do a TweenMax frame tween with an onComplete which may be better/easier than the ENTER_FRAME thing that keeps checking the frame numbers. See the Plugin Explorer for an interactive example of a frame tween. There are some examples in the forum too.

 

TweenMax.to(loader.rawContent, loader.rawContent.totalFrames - 1, {frame:loader.rawContent.totalFrames, useFrames:true, ease:Linear.easeNone}); 

Link to comment
Share on other sites

That's a brilliant idea, using TweenMax to play the swf. Not only do I understand it, it gets me around all the timer problems. Cool!

 

Thanks for your help

Link to comment
Share on other sites

Hi,

 

I am using the following code to load 3 external SWF files one after one. I have used the code replied by greensock. As I am new to greensock plug-ins, I have just copied the exact code as below on empty Flash CS4 AS 3 document's first frame. There is nothing on the page.

 

The code is:

 

var urls:Array = ["swfs/Logo.swf","swfs/Clock.swf","swfs/AnimatedBanner.swf"];

 

var curLoader:SWFLoader;

var curSWF:MovieClip;

 

playSWF(urls[0]);

 

function playSWF(url:String):void {

if (curLoader != null) {

curSWF = null;

curLoader.dispose(true);

removeEventListener(Event.ENTER_FRAME, checkFrame);

}

curLoader = new SWFLoader(url, {container:this, onComplete:completeHandler});

curLoader.load();

}

 

function completeHandler(event:LoaderEvent):void {

curSWF = curLoader.rawContent;

addEventListener(Event.ENTER_FRAME, checkFrame, false, 0, true);

}

 

function checkFrame(event:Event):void {

if (curSWF.currentFrame == curSWF.totalFrames) {

var index:int = urls.indexOf(curLoader.url) + 1;

if (index < urls.length) {

playSWF(urls[index]);

} else {

trace("done with all swfs");

}

}

}

Link to comment
Share on other sites

Did you have a question?

 

Make sure you've got the "com" folder from the zip download in the same directory as your FLA so that Flash can find the classes. And you do have the appropriate swfs in place that the script is looking for, right? ("swfs/Logo.swf","swfs/Clock.swf","swfs/AnimatedBanner.swf")

Link to comment
Share on other sites

Hi,

 

I checked the folder:

 

Example_Flash folder contains the following folders/files:

Demo.fla (File)

com (Folder)

swfs (Folder)

 

I am getting the following error message:

 

1046: Type was not found or was not a compile-time constant: LoaderEvent. (And it highlights the following line)

 

function completeHandler(event:LoaderEvent):void {

 

And it shows nothing on page.

 

Thanks.

Link to comment
Share on other sites

I may be wrong but it sounds like you haven't imported your greensock libraries?

Link to comment
Share on other sites

Please see the attached file. I have added the "com" folder to my fla file folder called "Demo".

Link to comment
Share on other sites

No no, rojharris wasn't talking about putting files on your hard drive - you need to put the import statements at the top of your ActionScript file/class/frame. Are you familiar with working with classes?

 

import com.greensock.loading.*;
import com.greensock.events.*;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×