Copy link to clipboard
Copied
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.
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;
...Copy link to clipboard
Copied
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?
}
}
Copy link to clipboard
Copied
Thank you.
Now I am trying to understand your script, but it's too hard.
var mcA:Array = a[Math.floor(Math.random()*a.length)];
Is it Array in Array? What this script does?
And one more.
playF();
function playF():void{
mc=mcA[index];
mc.addEventListener(Event.ENTER_FRAME,enterframeF);
}
mc=mcA means
MovieClip=mcA:Array = a[Math.floor(Math.random()*a.length)];
Is it right?
Copy link to clipboard
Copied
:
// 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?
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
}
}
}
Copy link to clipboard
Copied
Thank you. I really need to study more to understand your script.
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now