PDA

View Full Version : Random LoadMovie



kirupa
September 13th, 2001, 06:17 PM
Hi all,
I'm having a problem with an animation I'm working on. The code is listed below:

x = random(10);
trace (x);
if (x=1) {
&nbsp &nbsp &nbsp &nbsp loadMovie ("movie1.swf", "_root.a.b");
} else if (x=2) {
&nbsp &nbsp &nbsp &nbsp loadMovie ("movie2.swf", "_root.a.b");
} else if (x=3) {
&nbsp &nbsp &nbsp &nbsp loadMovie ("movie3.swf", "_root.a.b");
} else if (x=4) {
&nbsp &nbsp &nbsp &nbsp loadMovie ("movie4.swf", "_root.a.b");
} else if (x=5) {
&nbsp &nbsp &nbsp &nbsp loadMovie ("movie5.swf", "_root.a.b");
} else if (x=6) {
&nbsp &nbsp &nbsp &nbsp loadMovie ("movie6.swf", "_root.a.b");
} else if (x=7) {
&nbsp &nbsp &nbsp &nbsp loadMovie ("movie7.swf", "_root.a.b");
} else if (x=8) {
&nbsp &nbsp &nbsp &nbsp loadMovie ("movie8.swf", "_root.a.b");
} else if (x=9) {
&nbsp &nbsp &nbsp &nbsp loadMovie ("movie9.swf", "_root.a.b");
} else if (x=10) {
&nbsp &nbsp &nbsp &nbsp loadMovie ("movie10.swf", "_root.a.b");
} else {
loadMovie ("movie1.swf", "_root.a.b");
}

The 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.

Thanks!
Kirupa

kirupa
September 13th, 2001, 06:28 PM
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.

thoriphes
September 13th, 2001, 10:00 PM
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!"

now 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.

oh 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).

thoriphes
September 13th, 2001, 10:08 PM
oh, and it might make your code cleaner if you replace all that with this:

x = 0;
while (x == 0) {
x = random(11); //notice i have 11 here...
}
file = "movie"+x+".swf";
loadMovie(file, _root.a.b);

and 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.

suprabeener
September 14th, 2001, 02:22 AM
i'm more familiar with Math.random(); which returns a random number b/w 1 and 0. so for your purposes:

Math.random()*10;

and if you only want whole numbers:

Math.round(Math.random()*10);

i think the Math functions are encouraged as they use "real" math vs. emulated. i don't know what that means, just quoting the manual.

re: 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?

movies = ["gerbil.swf","dog.swf","lizard.swf" ... "giraffe.swf"];

loadMovieNum(movies[Math.round(Math.random()*movies.length)],2);

hmmm, looking back over this i'm not so sure the code quite qualifies as cleaner. ; )

moral of the story is: array's can be very handy.

incidentally, you most definitely should be using == for any testing.

kirupa
September 14th, 2001, 06:46 AM
thanks thoriphes and suprabeemer!
I'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.

Only 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.

Thanks,
Kirupa

thoriphes
September 14th, 2001, 09:15 AM
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?
anyway, 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.

kirupa
September 14th, 2001, 06:27 PM
Hey thoriphes and suprabeemer,
Thanks 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.

I 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.

Thanks!
Kirupa

thoriphes
September 14th, 2001, 10:34 PM
very nice backgrounds on the movies!

dan4885
September 18th, 2001, 12:54 AM
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?

kirupa
September 18th, 2001, 06:32 AM
I used thoriphes' code Dan:
-----------------------------
x = 0;
while (x == 0) {
x = random(11); //notice i have 11 here...
}
file = "movie"+x+".swf";
loadMovie(file, _root.a.b);
-----------------------------
The 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/ (http://www.kirupa.com/random/) before the word "movie" to make it load from that location wherever the animation may be on this site.

PiouPiou
September 19th, 2003, 03:24 AM
Could this code be adapted to play one of four frames in a movie clip randomly instead of an external swf file?

I am just learning and don't really have a handle on actionscript yet, but here is what I think the spirit of what I am trying to do is, I just can't get it to work:

x = 0;
while (x == 0) {
x = random(5);
}


if (x = 1) {
_root.backgrounds.gotoAndStop(2);
}
if (x = 2) {
_root.backgrounds.gotoAndStop(3);
}
if (x = 3) {
_root.backgrounds.gotoAndStop(4);
}
if (x = 4) {
_root.backgrounds.gotoAndStop(5);
}

Voetsjoeba
September 19th, 2003, 02:33 PM
_root.backgrounds.gotoAndStop(Math.round(Math.rand om()*5));

:)

PiouPiou
September 19th, 2003, 07:50 PM
OH MY GOD! SO SIMPLE! SO ELEGANT! YOU ARE A GENIUS!

Thank you so much - how'd you guys get to be so **** smart!?

Voetsjoeba
September 20th, 2003, 02:42 AM
You're welcome :)

I experimented, browsed this forum and learned a lot :)

dmitriysorokin
January 27th, 2009, 09:00 PM
I don't think keeping one movie on one server calling other movies from another server is going to work though. Flash has built in protection against that and will probably cause a Sandbox Violation error.

jpredig
May 17th, 2010, 01:11 PM
I'm trying to make a swf that plays other external swfs at random. Here is my code,

x = 0;
while (x == 0) {
x = random(5);
}
file = "movie"+x+".swf";
loadMovie(file,_root.a.b);

do i need a math.random() in there?

I have 5 movies all named movie1.swf movie2.swf movie3.swf and so on. they are in the directory with the "player.swf"