Loading a different image each time using Local Shared Objects - Page 4

Random Image

In order to generate random numbers in Flash you use Math.random(). This method returns a number x, where 0 <= x < 1.
Usually you want to generate a random integer n, where 0 <= n < max and max is also an integer.

For the non-repetitive random image, you need to make sure the new number is different from the last one. For this I created the following recursive function which ensures that the random number is always different from n. Obviously, total has to be greater than 1, i.e., you have to have more than one image, otherwise this doesn't make sense, and this code explodes!

This LSO has a different name - random - so that there are no conflicts with the previous example, but the rest of the code is practically the same. You just need to call randomNumber(). This is the code available in MainRandom.as:

// create a LSO identified by "random"
var lso:SharedObject = SharedObject.getLocal("random");
var current:uint;
// does "current" exist already
if (lso.data.current != undefined)
{
// grab it
current = lso.data.current;
// create a new one, different from the previous
current = randomNumber(current);
}
// if it doesn't, create a new one
else
{
// it can be any number
// all random numbers are different from -1
current = randomNumber(-1);
}
// store current in the LSO
lso.data.current = current;
// load the appropriate image
loadImage(current);

Everytime that you refresh the browser or test the movie, you'll get a different image from the previous one. In this case you can get image A, then B and then A again.

» Description:
I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all