Skip to main content
Inspiring
February 10, 2013
Answered

Play loop movies randomly but not completly randomly.

  • February 10, 2013
  • 1 reply
  • 1331 views

Hello, I am using Adobe Flash CS6 and Action Script3.

Right now I am trying to play movie clips randomly. There are 5 loop movie clips.mcA,mcB,mcC,mcD and mcE.

mcA is 10 frames long, mcB is 10 frames, mcC is 10 frames, mcD is 20 frames and mcE is 10 frames.

I said randomly, but not completely randomly.I want to play them in this order.

mcA -mcB

        -mcB -mcB

mcC - mcD - mcE

                  - mcD - mcE

                  - mcD - mcD - mcE

So, first random pick is mcA or mcC. If mcA is selected, next choice is mcB or mcB-mcB.

If mcC is selected, mcD follows, and next, mcE or mcD-mcE or mcD - mcD -mcE.

I try using Math.random, frame label, visibility, linkage to play them randomly, but I can't make it work correctly.

Do you have any idea?  Please help me. Thank you in advance.

This topic has been closed for replies.
Correct answer kglad

Thank you for your reply, but it won't work.

I got this error.

TypeError: Error #1034: Error forced conversion

I guess it comes from this line

var mcA:Array = a[Math.floor(Math.random()*a.length)];

If you can please tell me how to use this code.

This is the fla file I am working at.

http://www.dotup.org/uploda/www.dotup.org3939431.fla.


i used mcA for an array reference and you used mcA for a movieclip reference which caused that error:

// an array of arrays.  each element of a is allowable sequence of movieclips

var a:Array=[[mcA,mcB],[mcA,mcB,mcB],[mcC,mcD,mcE],[mcC,mcD,mcD,mcE],[mcC,mcD,mcD,mcD,mcE]];

// mc_A is a randomly chosen element of a.  ie, one of the allowable sequences of movieclips

var mc_A:Array=a[ Math.floor(Math.random()*a.length)]

// the movieclips in mcA will be played from the first until the last

var index:int=0;

var mc:MovieClip;

// playF starts the next movieclip in mcA playing (and as i'm typing this i realize i failed to add an mc.play() in playF)

playF();

function playF():void {

mc=mc_A[index];

// start it playing

mc.gotoAndPlay(2);

// if mc needs to be added to the stage or needs to be made visible etc, this is the place to do that

// add a loop to check when it finishes playing so we can start the next movieclip in mcA

mc.addEventListener(Event.ENTER_FRAME,enterframeF);

}

function enterframeF(e:Event):void {

// check if mc has completed play

if (mc.currentFrame==mc.totalFrames) {

// if so, remove the listener

mc.removeEventListener(Event.ENTER_FRAME,enterframeF);

// if mc no longer needs to be on stage or no longer needs to be visible etc, this is the place to do that

// increment the index anticipating starting the next movieclip in mcA

index++;

// if there's another mcA element left to play, play it

if (index<mc_A.length) {

playF();

} else {

// all done.  do something?

}

}

}

1 reply

kglad
Community Expert
February 10, 2013

just create an array of the allowable sequences and select one at random:

var a:Array=[[mcA,mcB],[mcA,mcB,mcB],[mcC,mcD,mcE],[mcC,mcD,mcD,mcE],[mcC,mcD,mcD,mcD,mcE]];

var mcA:Array = a[Math.floor(Math.random()*a.length)];

var index:int=0;

var mc:MovieClip;

playF();

function playF():void{

mc=mcA[index];

mc.addEventListener(Event.ENTER_FRAME,enterframeF);

}

function enterframeF(e:Event):void{

if(mc.currentFrame==mc.totalFrames){

mc.removeEventListener(Event.ENTER_FRAME,enterframeF);

index++;

if(index<mcA.length){

playF();

} else {

// all done.  do something?

}

}

kglad
Community Expert
February 11, 2013

:

// an array of arrays.  each element of a is allowable sequence of movieclips

var a:Array=[[mcA,mcB],[mcA,mcB,mcB],[mcC,mcD,mcE],[mcC,mcD,mcD,mcE],[mcC ,mcD,mcD,mcD,mcE]];

// mcA is a randomly chosen element of a.  ie, one of the allowable sequences of movieclips

var mcA:Array = a[Math.floor(Math.random()*a.length)];

// the movieclips in mcA will be played from the first until the last

var index:int=0;

var mc:MovieClip;

// playF starts the next movieclip in mcA playing (and as i'm typing this i realize i failed to add an mc.play() in playF)

playF();

function playF():void{

mc=mcA[index];

// start it playing

mc.play();

// if mc needs to be added to the stage or needs to be made visible etc, this is the place to do that

// add a loop to check when it finishes playing so we can start the next movieclip in mcA

mc.addEventListener(Event.ENTER_FRAME,enterframeF);

}

function enterframeF(e:Event):void{

// check if mc has completed play

if(mc.currentFrame==mc.totalFrames){

// if so, remove the listener

mc.removeEventListener(Event.ENTER_FRAME,enterframeF);

// if mc no longer needs to be on stage or no longer needs to be visible etc, this is the place to do that

// increment the index anticipating starting the next movieclip in mcA

index++;

// if there's another mcA element left to play, play it

if(index<mcA.length){

playF();

} else {

// all done.  do something?

}

}

Inspiring
February 12, 2013

Thank you for your reply, but it won't work.

I got this error.

TypeError: Error #1034: Error forced conversion

I guess it comes from this line

var mcA:Array = a[Math.floor(Math.random()*a.length)];

If you can please tell me how to use this code.

This is the fla file I am working at.

http://www.dotup.org/uploda/www.dotup.org3939431.fla.