The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

1. kirupa
n/a
posts
Guest

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 {
}

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

2. kirupa
n/a
posts
Guest

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.

3. thoriphes
n/a
posts
Guest

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

4. thoriphes
n/a
posts
Guest

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";

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.

5. suprabeener
n/a
posts
Guest

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"];

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.

6. kirupa
n/a
posts
Guest

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

7. thoriphes
n/a
posts
Guest

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.

8. kirupa
n/a
posts
Guest

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

9. thoriphes
n/a
posts
Guest

very nice backgrounds on the movies!

10. dan4885
n/a
posts
Guest

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?

11. kirupa
n/a
posts
Guest

I used thoriphes' code Dan:
-----------------------------
x = 0;
while (x == 0) {
x = random(11); //notice i have 11 here...
}
file = "movie"+x+".swf";
-----------------------------
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/ before the word "movie" to make it load from that location wherever the animation may be on this site.

12. 70
posts
Registered User
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);
}
Last edited by PiouPiou; September 19th, 2003 at 02:46 AM.

13. Code:
`_root.backgrounds.gotoAndStop(Math.round(Math.random()*5));`

14. 70
posts
Registered User
OH MY GOD! SO SIMPLE! SO ELEGANT! YOU ARE A GENIUS!

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

15. You're welcome

I experimented, browsed this forum and learned a lot

Page 1 of 2 12 Last

There are currently 1 users browsing this thread. (0 members and 1 guests)