Damian Shine Damian Shine - 9 months ago 91
ActionScript Question

Actionscript, load random Movie Clip into Scene

I'm making a simple flash shooting gallery animation with about 5 targets on screen but I am useless at action script.

I have the main scene and 5 Target movie clips in an array. I would like to...
-> Start Animation -> load random clip -> play random clip till end-> generate new random clip -> Repeat with Delay Offset....

So far I have the following:

function getRandomLabel():String {
var labels:Array = new Array("Tar1", "Tar2", "Tar3", "Tar4", "Tar5");

var index:Number = Math.floor(Math.random() * labels.length);
return labels[index];
}
this.gotoAndStop(getRandomLabel());
}


This is working... but I would like to add a delay and no repeat to this...

Answer Source

Ok, lets do it.

// If you need to avoid playing the same movie two times.
var lastLabel:*;

// The list of labels.
var Labels:Array = ["Tar1", "Tar2", "Tar3", "Tar4", "Tar5"];

function playRandom():*
{
    do
    {
        // Get a random index.
        var anIndex:int = Math.random() * Labels.length;
    }
    while (Labels[anIndex] == currentLabel);

    // Keep the current label in the variable.
    currentLabel = Labels[anIndex];
    gotoAndStop(currentlabel);
}

function playNext():void
{
    // 1000 milliseconds = 1 second delay.
    setInterval(playRandom, 1000);
}

Then. At the end of each of your movie clips you need to correctly call the playNext method. If these movies are in the same timeline, as the code above, just call playNext(); If they are separate MovieClip objects, it will probably be (parent as MovieClip).playNext(); I cannot really tell because I don't know the structure of your movie. You will probably need to read the following to understand: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e3e.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download