Search code examples
actionscript-3timerinstancemovieclip

Randomly playing multiple instances of movieclip AS3


I have made a movieclip of an eye, which is set to play a blinking animation at random intervals. I want to place many instances of that movieclip on the stage, where they're all being played at different times. But if I put multiple instances of the clip on the stage, only one will play. How do I have many instances playing at the same time?

I haven't used flash for years so please explain simply if you can~

stop();
var eyeTimer = new Timer(1000 + Math.random() * 1000);
eyeTimer.addEventListener(TimerEvent.TIMER, blink);
eyeTimer.start();
function blink(evt:TimerEvent):void {
  Timer(evt.currentTarget).delay = 1000 + Math.random() * 1000;

 trace( "triggered!" );
 eye.gotoAndPlay(1);
}

Solution

  • Its easier to use setTimeOut method. user below code for your Eye MovieClip

    addEye (45,78);
    addEye (76,44);
    ...
    
    function addEye (X,Y)
    {
        var eye:Eye = new Eye ();
        eye.x = X;
        eye.y = Y;
        this.addChild(eye);
    
        eye.gotoAndStop(1);//Stop the eye from blinking. may be on frame 1 maye be on other frame number.
        setTimeOut(blink,1000+Math.random()*1000);
        function blink():void
        {
            eye.gotoAndPlay(2);
            setTimeOut(blink,1000+Math.random()*1000);//Call the random function with random time again
        }
    
    }