views:

1173

answers:

1

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!

A: 

I work with AS3 these days so that is the explanation I will give.

There are many ways you could go about this. You could use a Tweening engine, such as Tweenlite, one of my favorites, and use its onComplete parameter, or you could merely create a function that creates a timer and sets a random wait to such and then upon completion of the wait, your animation fires; Once your animation fires, it dispatches an event, which is being listened for on each of your coin movieclip animations.

mc1.addEventListener(Event.COMPLTE, _onComplete);

private function _onComplete(e:Event):void
{
    var target:MovieClip = e.target;
    var timer:Timer = new Timer(Math.random() *  5 + 5, 1); //Returns a number between 5 and 10
    timer.addEventListener(TimerEvent.TIMER, onTimer);
    timer.start();
    function onTimer(e:TimerEvent):void
    {
        target.play();
    }
}

Now all we have to do is dispatch Event.COMPLETE from the last frame of all your coin animations.

As for how you want to start off your randomization that is up to you. By default they will all start playing on their own unless you've added a stop action, so the first time they will all be in sync, but will then begin to stagger. To randomize the beginning you must put stop actions at the beginning of each, then i would iterate through the container clip telling each child to start playing after a random number of seconds, as we have done above.

hodgedev.com

Brian Hodge