PDA

View Full Version : randomly load swf?


stompwampa
11-05-2007, 07:05 PM
I have a master swf that will load different banner ads. I have three URL resuests for the three banner ads. I want the loader to randomly load one of the URL requests each time the swf plays...
not sure how to go about it.

I can make the loader and the URL requests, and I can make a random number generator...but how can I make it so the number generate (between 1 - 3) will correspond to one of the URL requests, and thus load the associated swf file??

I think I can do it with some IF/ELSE statements, but it seems there would be an easier way...

ryryguy
11-05-2007, 07:27 PM
Use an array to store the URL requests.

var requests:Array = [new URLRequest(...), new URLRequest(...), new URLRequest(...)];
var randomRequest:URLRequest = requests[randUint(requests.length)];

(Where randUint returns a random uint between 0 and the argument minus one.)

Here, as a bonus, is the implementation or randUint:
/**
* Returns a random uint in the range 0 <= number < max
*
* @param max The returned number will be less than this upper bound
* @return A random number less than max
*/
public static function randUint(max:uint):uint
{
return Math.floor(Math.random() * max);
}

stompwampa
11-05-2007, 08:28 PM
Thanks, man.
I ended up using the IF/ELSE statments I mentioned in my OP...it only turned out to be like ten lines of code...not nearly as much as I thought it would.
It works great too.
Thanks!