How to randomly loop a swf array continuously and each swf looping a random # of times?
Copy link to clipboard
Copied
I need some help to continuously loop an array of swfs randomly, while also looping each chosen swf a random number of times, and depending on which file is chosen a specific transition swf needs to play once. Can anyone help with this in as 3.0 code or point me in the direction of a tutorial, I'm using Flash Pro CS6. I have found a few tutorials on looping swfs but nothing cohesive enough to know the best way to tackle this.
here is a tutorial I did find: http://www.actionscript.org/forums/showthread.php3?t=239856
thanks.
Copy link to clipboard
Copied
You are not going to find a tutorial for this due to its very specific focus, but you can find info for the pieces you need.
Can you show what you have tried so far?
To random loop an array of anything what you normally do is shuffle that array and then play thru it from start to end. If you search this forum using the word "shuffle" you should easily find a solution for that.
To randomly dictate how many times an swf loops depends on how the swf's are made. It helps if they are made to suit the need, where you first define that random number of loops using the Math.random() function with a rounding function to give an integer result, then you assign that value to the swf which uses it to control how many times it loops.
Copy link to clipboard
Copied
Hi Ned,
Thanks - the word 'shuffle' helped, although as you might see from my post below, I'm not sure how to integrate the random/shuffle code into the loading code.
As for the swf's - they are timeline animations created in flash. I'll take some time to read over what you've said and try to test it out.
Copy link to clipboard
Copied
I don't see an array in your code, so that would be a good place to start... create one using the swf file names. Then shuffle that array and use it for your loading code.
If the swf's are timeline animations, then all you should need to do is have a couple variables in the timeline of each, one to keep a count of the loops and one to sassign the total number of loops. When the loop count = the total count, you stop the looping and load the next swf. Create these variables in the first frame and for the loop counter, just declare it, don't assign it. That way when you return to the frame it will retain whatever value you assign to it as the loops increment. In your last frame you increment that counter and compare it to the total to see if you need to play() or stop()/load next.
Copy link to clipboard
Copied
1.Sorting an array randomly:
function randomSort(a:*, b:*):Number
{
if (Math.random() < 0.5)
return -1;
else
return 1;
}
//usage: Array.sort(randomSort);
2.Getting a random Integer:
function _randInt(min:int, max:int):int { return int((Math.random() * (max - min + 1)) + min); }
//usage for getting a randomNumber between 1 & 10: num = _randInt(1,10);
3.Use the code example you linked to above or look into a framework that will let you load multiple swf at once
4.Combine the 3
5.Profit
Copy link to clipboard
Copied
Hi - thanks for the code and framework link, as I am still learning AS I was not sure about jumping into using Loadermax but I found their code to load multiple swfs which works nicely - but now I don't know how to intergrate the randomsort code?
Here is the code I found, can I add the randomsort code above into this? Thanks for your help.
import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
progress_mc.scaleX = 0;
var loaderIndex:Number = -1;
var currentLoader:SWFLoader;
var swfs:LoaderMax = new LoaderMax({onComplete:completeHandler,onProgress:progressHandler,onChildComplete:childCompleteHandler});
swfs.append(new SWFLoader("Stop/SWF0.swf", {container:container_mc, autoPlay:false}));
swfs.append(new SWFLoader("Stop/SWF1.swf", {container:container_mc, autoPlay:false}));
swfs.append(new SWFLoader("Stop/SWF2.swf", {container:container_mc, autoPlay:false}));
function progressHandler(e:LoaderEvent):void
{
progress_mc.scaleX = e.target.progress;
}
function childCompleteHandler(e:LoaderEvent):void
{
trace(e.target + " loaded");
e.target.content.visible = false;
}
function completeHandler(e:LoaderEvent):void
{
trace("all swfs loaded");
progress_mc.visible = false;
initCurrentLoader();
addEventListener(Event.ENTER_FRAME, trackSWFPlayback);
}
function initCurrentLoader()
{
loaderIndex++;
if (loaderIndex == swfs.numChildren)
{
//reset back to 0 if the last swf has already played
loaderIndex = 0;
}
//dynamically reference current loader based on value of loaderIndex
currentLoader = swfs.getChildAt(loaderIndex);
//make the content of the current loader visible
currentLoader.content.visible = true;
//tell the current loader's swf to to play
currentLoader.rawContent.gotoAndPlay(1);
}
function trackSWFPlayback(e:Event):void
{
//trace(currentLoader.rawContent.currentFrame);
//detect if the loaded swf is on the last frame
if (currentLoader.rawContent.currentFrame == currentLoader.rawContent.totalFrames)
{
trace("swf done");
//hide and stop current swf
currentLoader.content.visible = false;
currentLoader.rawContent.stop();
//set up and play the next swf;
initCurrentLoader();
}
}
load_btn.addEventListener(MouseEvent.CLICK, loadSWFs);
function loadSWFs(e:MouseEvent):void
{
load_btn.visible = false;
swfs.load();
}

