1 Reply Latest reply on May 18, 2006 4:24 PM by blemmo

# Calling a random MC

Hi,
I have five MC's that I made and would like to call one of them up with a button action. When one pops up I would like it to choose from one of the five randomly. So, when you click the button, a mc animation will play. If you click another button, it calls another (or the same, since it is random) mc animation and makes it play.

How do I do this?

Thanks.
Van
• ###### 1. Re: Calling a random MC
There are various ways to do that. All of them involve Math.random(), check it out in the Help. This returns a random number between 0 and 1. Because you want a random number between, let's say 1 and 5, you have to do some calculation with the result, like
randNum = Math.round(Math.random()*4+1)
to get a number in that range. There's a better example in the Help how to get numbers in a certain range.
Now this number can be used for loading the clips. Simplest way would be naming the MCs "mc1", "mc2" and so on, and use
loadMovie("mc"+randNum+".swf").
If the names are not in that style, you could use a switch statement:
switch(randNum){
case 1: loadMovie("xxx.swf"); break;
case 2: loadMovie("abc.swf"); break;
...
It's also possible to have an array containing references to the MCs, and load the MC at the randNum position:
array = ["xxx.swf","abc.swf",...];
loadMovie(array[randNum]);

hth,
blemmo