I have four Flash animations for the home page of my web site. Each time a user accesses the site, I need one of the four animations to randomly load.
I originally thought this would be done making four separate .swf’s (one for each animation), but I’ve heard it would be better to put all four in one .swf and use ActionScript to randomly generate where to start on the timeline, to randomly show one of the animations.
I’m new to ActionScript and would really appreciate any help with code I’ll need to achieve this. Thank you much!
I couldn’t really say if you should have them all in one swf or seperate…. probably depends on the filesize of said animations. If they are large no need forcing your user’s to download all 4 just to see 1 of them.
But if you do put them all on the same timeline, you could use labels…. make lables like anim1, anim2, anim3, anim4
/*--- Taken from Flash Help Doc's ---*/
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
gotoAndPlay("anim"+randRange(1,4));
Or if you want to load seperate .swf’s
/*--- Taken from Flash Help Doc's ---*/
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
loadMovie("anim"+randRange(1,4)+".swf");
- 19 February 2008 06:12 PM
-
Author
It worked! Thanks for pointing me in the right direction!
I ended up creating a 5-frame .swf. In the first fram I put this code…
Code:
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
for (var i = 0; i < 4; i++) {
var n:Number = randRange(1, 4)
}
gotoAndPlay(“anim”+randRange(1,4));
In frames 2 through 5, I named them anim1 through anim4, and I did a loadMovie of each of the four animations.
Works great. Thanks again for your help.
-e
- 19 February 2008 09:03 PM
-
Author
Check this out. Now that I have a proof of this posted on our FTP server, it’s not evenly distributing the four animations. In fact, it appears Firefox plays only three of them, and Explorer plays only three of them (but not the same three as Firefox). I’m completely baffled. Has anyone else run in to this problem or have any idea why it’s not generating a truely random selection? Thanks!
- 20 February 2008 02:02 PM
-
I’ve done this pretty successfully a few times using random swfs. That way only one swf loads - one site is www.rawhideco.com. If you want the code get back to me. regards, David.
- 20 February 2008 02:09 PM
-
- Log in or join for free to make a comment.


