This content has been marked as final. Show 3 replies
Run a Math.random() call to generate a random number. All the images that will be in the background should be numbered numerically. Use loadMovie or MovieClipLoader (AS2) or Loader (AS3) to load in your image and get it on screen.
Hi thanks for your reply. I've a little look at some other tuts that use Math.random() but i am unsure how i would adapt it to my requirements. Please forgive my ignorance i'm a bit of a newbie with actionscript. would you mind explaining a little further? thanks.
Sure. The basic idea is simple. You want to load a random image. Flash only provides one means of generating anything random and it's the random() method of the Math class.
By itself, random() is only going to help you generate a number. But if you take it one step further and append that number result to ".jpg", what you'll get is a String pointing to the filename of an image in this case 1.jpg. For instance
// Math.random() returns a 1 stored in a variable named pic
var myFile:String = pic + ".jpg";
Now you can use loadMovie, MovieClipLoader (both AS2) or the Loader class (AS3) to load the file onto the stage. I'll show MovieClipLoader and Loader
// MovieClipLoader - assumes images loading are in same directory as swf
var myMCL:MovieClipLoader = new MovieClipLoader();
myMCL.loadClip( myFile );
// Loader - assumes same as above
var _ldr:Loader = new Loader();
addChild( _ldr );
_ldr.load( new URLRequest( myFile ) );
The only code you need to control is the Math.random() so that you put an upper and lower bound on the returned result. You can find help with that inside the Flash documentation.
Hope this helps.