Random LoadMovie

Hi all,\rI’m having a problem with an animation I’m working on. The code is listed below:\r\rx = random(10);\rtrace (x);\rif (x=1) {\r&nbsp &nbsp &nbsp &nbsp loadMovie (“movie1.swf”, “_root.a.b”);\r} else if (x=2) {\r&nbsp &nbsp &nbsp &nbsp loadMovie (“movie2.swf”, “_root.a.b”);\r} else if (x=3) {\r&nbsp &nbsp &nbsp &nbsp loadMovie (“movie3.swf”, “_root.a.b”);\r} else if (x=4) {\r&nbsp &nbsp &nbsp &nbsp loadMovie (“movie4.swf”, “_root.a.b”);\r} else if (x=5) {\r&nbsp &nbsp &nbsp &nbsp loadMovie (“movie5.swf”, “_root.a.b”);\r} else if (x=6) {\r&nbsp &nbsp &nbsp &nbsp loadMovie (“movie6.swf”, “_root.a.b”);\r} else if (x=7) {\r&nbsp &nbsp &nbsp &nbsp loadMovie (“movie7.swf”, “_root.a.b”);\r} else if (x=8) {\r&nbsp &nbsp &nbsp &nbsp loadMovie (“movie8.swf”, “_root.a.b”);\r} else if (x=9) {\r&nbsp &nbsp &nbsp &nbsp loadMovie (“movie9.swf”, “_root.a.b”);\r} else if (x=10) {\r&nbsp &nbsp &nbsp &nbsp loadMovie (“movie10.swf”, “_root.a.b”);\r} else {\r loadMovie (“movie1.swf”, “_root.a.b”);\r}\r\rThe problem is that everytime the movie only plays the movie corresponding to x = 1. In Flash, I used trace(x) and pressed Ctrl + Enter. The Output Window displayed numbers between 1 through 10. I then tried to create a textfield corresponding to x, and then previewed the animation in my browser. The value was still coming up as 1. I’m not sure why Flash is able to display numerous random numbers, yet the browser is only displaying 1. \r\rThanks!\rKirupa

I have even tried replacing the ‘=’ with ‘==’ as per AS guidelines. This will be for the logo you see on the top of this site. The image to the right of kirupa.com will randomly load one of 10 vectorized images that are each about 2-4k each. It should help increase download speeds as well as provide something interesting for people to notice; if anybody notices at all lol.

well, you’re right on changing the “=” to “==”. that was the first thing i immediately noticed. secondly, what’s in _root.a.b? i’m guessing a variable. anyway, whenever you use _root, you keep it as _root, not “_root”. the former acts as an expression whereas the latter is a string, like “Hello World!”\r\rnow i could be wrong about the _root thing having quotes or not, because i don’t really use loadMovie, but for everything else in AS, that’s the case.\r\roh and one last thing, the reason your browser reads it as 1, is because you assign it “x=1” in your if statement. it should read if (x==1).

oh, and it might make your code cleaner if you replace all that with this:\r\rx = 0;\rwhile (x == 0) {\r x = random(11); //notice i have 11 here…\r}\rfile = “movie”+x+".swf";\rloadMovie(file, _root.a.b);\r\rand that’s pretty much it. oh and btw, i must clarify something, with random, whatever number you put in the parens, that’s the MAX value it won’t use. confused? well, random(10) will only randomize 0-9. random(11) will go to 10 as well. also to avoid x being 0, i put that while loop there.\r

i’m more familiar with Math.random(); which returns a random number b/w 1 and 0. so for your purposes:\r\rMath.random()*10;\r\rand if you only want whole numbers:\r\rMath.round(Math.random()*10);\r\ri think the Math functions are encouraged as they use “real” math vs. emulated. i don’t know what that means, just quoting the manual.\r\rre: cleaner code. another method you might consider is storing you movie names in an array. this way you can name them whatever you please - perhaps something descriptive, n’est pas?\r\rmovies = [“gerbil.swf”,“dog.swf”,“lizard.swf” … “giraffe.swf”];\r\rloadMovieNum(movies[Math.round(Math.random()*movies.length)],2);\r\rhmmm, looking back over this i’m not so sure the code quite qualifies as cleaner. ; )\r\rmoral of the story is: array’s can be very handy.\r\rincidentally, you most definitely should be using == for any testing.

thanks thoriphes and suprabeemer!\rI’ll give it a shot once I come back from school today. The _root.a.b is the movie clip in which I am loading the movies into. If I automatically set x = 4 or x = 5 instead of Random(10) the correct movie loads without a problem.\r\rOnly with the Random function does the movie stall. I used normal mode to create the LoadMovie command, but I will try out the methods you both have mentioned.\r\rThanks,\rKirupa

hm…the random function never really should stall the program unless it’s stuck in an infinite loop of some sort. did it stall with the code i gave you?\ranyway, suprabeemer, you do have a good point on the arrays and descriptiveness. i like using random over Math.random cause i don’t have to use the extra *10 and Math.round junk. random gets to the point.

Hey thoriphes and suprabeemer,\rThanks for the tips. The animation worked perfectly. The code you gave me worked quite nicely thoriphes. I have to learn more in Flash programming, but I have been too lazy of late to touch Colin Moock’s ASDG. \r\rI believe you should be able to see the changes in the header animation. The image is different each time when the page is re-loaded.\r\rThanks!\rKirupa

very nice backgrounds on the movies!

Hey Kirupa if you wouldn’t mind could you paste me the exact AS u used to make it work I was working on this myself and I never got it to work! Did you just have dynamic images to show up depending on a random number 1-10 or did you use MC’s or .swf’s?

I used thoriphes’ code Dan:\r-----------------------------\rx = 0;\rwhile (x == 0) {\rx = random(11); //notice i have 11 here…\r}\rfile = “movie”+x+".swf";\rloadMovie(file, _root.a.b);\r-----------------------------\rThe images were loaded from a separate SWF file named movie1, movie2, movie3, etc. So that is why, movie + x + .swf works in loading the movies. I added www.kirupa.com/random/ before the word “movie” to make it load from that location wherever the animation may be on this site.