Kircho
Kircho

Reputation: 71

pay random movie on every 4 seconds as3

I have 5 MovieClips ( 5 simple buttons ) and i need them play one of them in every 4 seconds for ever, from second frame. What I'm doing wrong ? Thanks.

var clipArray:Array = new Array();



clipArray[0] = loader.button_01_mc.gotoAndPlay (2);
clipArray[1] = loader.button_02_mc.gotoAndPlay (2);
clipArray[2] = loader.button_03_mc.gotoAndPlay (2);
clipArray[3] = loader.button_04_mc.gotoAndPlay (2);
clipArray[4] = loader.button_05_mc.gotoAndPlay (2);




var clipTimer:Timer = new Timer(4000);
clipTimer.addEventListener(TimerEvent.TIMER, playClips);



function playClips(event:TimerEvent):void
{
    //Chooses a random clip in your array

     var randomClip:int = Math.random() * clipArray.length;

}



clipTimer.start();

Upvotes: 0

Views: 265

Answers (1)

Christophe Herreman
Christophe Herreman

Reputation: 16085

You need to store references to your movieclips in that array instead of the result (which is void) of a gotoAndPlay method call. Then get one of the clip from the array using a random index and call the gotoAndPlay method on it.

var clipArray:Array = new Array();
clipArray[0] = loader.button_01_mc;
clipArray[1] = loader.button_02_mc;
clipArray[2] = loader.button_03_mc;
clipArray[3] = loader.button_04_mc;
clipArray[4] = loader.button_05_mc;

var clipTimer:Timer = new Timer(4000);
clipTimer.addEventListener(TimerEvent.TIMER, playClips);

function playClips(event:TimerEvent):void {
  var randomClip:int = Math.floor(Math.random() * clipArray.length);
  var mc:MovieClip = clipArray[randomClip];
  mc.gotoAndPlay(2);
}

clipTimer.start();

Upvotes: 2

Related Questions