I recently created a Flash Animation that would randomly play one of three different Flash scenes. Then after each scene, it would go back to the beginning and randomly choose another scene to play. This was all programmed in ActionScript in Flash.
I have a feeling that even though this code is very simple, it will save a little bit of time if I have it stored here for the future.
// First Choose a random number
//from 0 to the number of scenes
num = random(3);//Depending on which random number
//was chosen, go to a flash scene
switch (num)
{
case 0:
gotoAndPlay(”scenename”, 1);
break;
case 1:
gotoAndPlay(”scenename1″, 1);
break;
case 2:
gotoAndPlay(”scenename2″, 1);
break;
default:
trace ( ” no case tested true ” )
}
That’s it!
-Ashton Sanders
(P.S. If this was useful for you, please leave a comment!
)







hi ashton, looks good i was looking some like this. where do we put this script? in the first scene?
Comment by dizitalART — March 13, 2008 @ 7:58 pm
Hi,
Yes. Make the first scene a blank screen, and put this code in one of the frames.
Then have the end of each of your scenes redirect back to the beginning of the first scene.
Comment by Ashton Sanders — March 14, 2008 @ 4:14 pm
Hi Ashton,
I added the code to the end of each scene (3 scenes total) where I previously had code to go to the next scene. But this seems to repeat quite frequently. Do you know a way to prevent repeating schne?
Comment by Jeff — April 6, 2009 @ 9:17 am
Hi Jeff,
Thanks for the question. Yes, to make the scenes not repeat, do not put the current scene in the randomizing code.
For example, for the first scene, don’t have “GotoAndPlay(”scenename”, 1);” You’ll have to change the code to be for two scenes since you are removing one of them. That way, there is no chance to play the scene you are already in.
I hope that helps,
Ashton Sanders
Comment by Ashton.Sanders — April 6, 2009 @ 10:06 am
Niiiiiiice! Exactly what I was looking for. Using with GotoAndStop to randomly display different call-to-action hero images on a home page. THX
Comment by OmegaTech — August 8, 2010 @ 5:38 am