R – Flash : Load random movie clips into animation

actionscriptactionscript-3flash

I'm using AS2, but I could also do it in AS3.

I'm making a simple animation with about 10 "coins" on screen. I have a movie clip that animates another movie clip flipping over. I want to pull a random movie clip from the library into the nested clip so that on each "flip" a random coin face comes up.

I've put all the clip names into an array (coin1,coin2,coin3,etc.)

I think it would be described as _root.coin_container.coin_animation.random_coin_here

There will be 10 coin_container's on the main stage, with coin_animation nested inside. At the beginning of the animation a random movie clip from the array should be called into coin_animation, then coin_animation will run through a few frames, repeat, call another random movie clip and repeat.

Additionally if I could set a random time for the animation to pause so the 10 animations are flipping randomly that would be nice.

I hope that is clear enough.
Thanks!

Best Answer

An as2 example. Paste this into an frame 1 of an empty flash file, create a movieclip called "coin" and via properties set its linkage identifier to "coin". You can adapt this for what you need. No need for several frames...

var numCoins:Number = 10;
var coins:Array = new Array();
var offset:Number = 500;

//add coins to the stage
addCoinsToStage(this);

//shuffle the order
fisherYatesShuffle(coins);

//play them one by one with a random offset
playMovies(coins, offset);

function addCoinsToStage(obj:Object):Void{
    for(var i:Number = 0; i < numCoins; i++){
        obj.attachMovie("coin", "coin"+i, obj.getNextHighestDepth());
        coins.push(obj["coin"+i]);
        //position on stage
        coins[i]._x = Math.random() * Stage.width;
        coins[i]._y = Math.random() * Stage.height;
    }
}


function fisherYatesShuffle(arr:Array):Void{
    for(var i:Number = arr.length - 1; i > 1; i--){
        var j = Math.round(Math.random() * i);
        var temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }   
}

function playMovies(arr:Array, delay:Number){   
    for(var i:Number = 0; i < arr.length; i++){
        var d = i * delay;
        trace(d);
        setTimeout(playMovie, d, arr[i]);
    }
}

function playMovie(movie:MovieClip){
    movie.play();
}

You will need to create the coin movieclip correctly. You should have a stop(); on frame 1 and on the last frame a blank empty keyframe that has gotoAndPlay(2);

Related Topic