Ultrashock Forums > Flash > ActionScript
AS2 - Random Display

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
AS2 - Random Display
Old 2008-02-19

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!
postbit arrow 4 comments | 1320 views postbit arrow Reply: with Quote   
Registered User
edub70 is offline
seperator
Posts: 15
2007-07-13
Age: 31
edub70 lives in United States
seperator

Ultrashock Member Comments:
sims11tz's Avatar sims11tz sims11tz is offline sims11tz lives in United States 2008-02-19 #2 Old  
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
Code:
/*--- 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
Code:
/*--- 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");
Reply With Quote  
edub70 edub70 is offline edub70 lives in United States 2008-02-19 #3 Old  
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
Reply With Quote  
edub70 edub70 is offline edub70 lives in United States 2008-02-20 #4 Old  
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!
Reply With Quote  
dbeechey dbeechey is offline 2008-02-20 #5 Old  
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.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: