Jump to content
GreenSock

ash allen

randomly looping swf files continuously

Moderator Tag

Recommended Posts

Hi - I'm new to using greensock (and actionscript) and am trying to play around with some code I found. It originally loaded several swf files and looped through them continuously  - I'm trying to randomize it.  I have created an array and added code to shuffle through the array, but I'm stuck trying to integrate it into the looping files.  I'm pretty sure I have to do something with the loaderIndex, but I'm not sure what.

 

Any help would be great - thanks.

 

Code:

 

progress_mc.scaleX = 0;

var loaderIndex:Number = -1;
var currentLoader:SWFLoader;
var urls:Array = ["SWF0.swf","SWF1.swf","SWF2.swf"];
var swfs:LoaderMax = new LoaderMax({onComplete:completeHandler,onProgress:progressHandler,onChildComplete:childCompleteHandler});

// Loop through urls and create a queue of swf loaders
for (var i:Number = 0; i < urls.length; i++)
{
    swfs.append( new SWFLoader(urls, {container:container_mc, autoPlay:false}) );
}
urls.sort(randomSort);


function randomSort(a:*, b:*):Number
{
    if (Math.random() < 0.5)
    {
        return -1;
    }
    else
    {
        return 1;
    }
}

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

Link to comment
Share on other sites

Have you tried sorting the urls before creating the loaders and appending them to the LoaderMax?

urls.sort(randomSort); // do this first

// Loop through urls and create a queue of swf loaders
for (var i:Number = 0; i < urls.length; i++)
{
    swfs.append( new SWFLoader(urls[i], {container:container_mc, autoPlay:false}) );
}
  • Like 1
Link to comment
Share on other sites

Hi - thanks, yes that did help loop the files, but didn't quite achieve what I wanted, and now I look at the code I realise I'm tackling what I want to do the wrong way, so I'm going back to the drawing board!

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.
×