-
I'm attempting to load an external swf file at random.
To accomplish this I was hoping to put the paths of the swf files in an array and then call random to generate the random #. Then use the loadMovie command to load the selected file.
I've learned that loadMovie doesn't seem to like arrays or even variables in the arguments.
Example.
var MaxClips = 4;
var RandNum = random(MaxClips);
myClips = new Array();
myClips [0] = "0.swf";
myClips [1] = "1.swf";
myClips [2] = "2.swf";
myClips [3] = "3.swf";
// now for loading the movie
loadMovie(myClips[RandNum], 2);
-- or --
myMovieClip = myClips[RandNum];
loadMovie(myMovieClip, 2);
Later in the movie the user will be able to select options to change the loaded movie and again, I use the array.
Any help is appreciated.
- Keith
-
If I am right, the problem lies in the calling of the swf out of the array. The array wants an integer, but your random variable is not likely to be. You need to use round your random number with the math.round() before you put that number into the array to get your SWF. Something like...
Code:
maxclips = 4;
randomnum = (Math.random()*maxclips+1)-0.5;
clipnum = Math.round(randomnum);
myclips = newArray("0.swf","1.swf","2.swf","3.swf");
//loading the movie
loadMovie(myclips[clipnum],2);
I have added an adjustment to the random number to compensate for the rounding. Flash will round to the nearest integer, so any numbers from -0.5 to 0.5 get rounded to 0 and any number from 3.5 to 4.5 get rounded to 4. Without the correction, numbers will be 0 at least and 4 at most. In short, without correction the numbers 0 and 4 will not occur as often as 1,2, or 3.
-
I'm not really sure how that should matter. I've done other array calls using random and they worked. According to the ActionScript Dictionary, random() returns an integer.
Even if I use loadMovie(myClips[1], 2); it still doesn't work.
But, I tried what you did anyway, same problem still exists. Thanks for responding though.
It seems like maybe loadMovie can not accept a variable as an argument. I'm hoping there is a way around this or that maybe it can work, but there's something else I need to do.
Thanks,
- Keith
-
Problem Solved
Found an answer to this problem on Macromedia's USENET Newsgroup, macromedia.flash.
Someone else posted the same question, the solution provided was so simple I don't know why I didn't notice it.
Instead of using loadMovie, use loadMovieNum(arrayname[randnum],2);
I tried it and it works.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|