3 Replies Latest reply on Sep 13, 2007 7:36 AM by SymTsb

    load a different image to stage

    Stefan83
      Hi there,
      I am trying to place an image on the homepage of a website i am building but i want the image to change every time the page loads. could anyone give me an idea on how to set this up or know of a good tutorial? my client is a photographer so they want to display a different photo every time the page reloads. i also want the anchor point of the image to be at the top left so the image begins at the same place. Any ideas? thanks in advance.
        • 1. Re: load a different image to stage
          SymTsb Level 2
          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.
          • 2. Re: load a different image to stage
            Stefan83 Level 1
            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.
            • 3. Re: load a different image to stage
              SymTsb Level 2
              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.