How to use ActionScript to load a random picture onto a web page.

Home • Members Tutorials Forum iSnapChat Contact Us 


Flash Tutorials


Using Flash to Load a Random Image


Free Flash Tutorial

Step 3    <<   Previous      Intro   1   2   3    >>      


Step Three: Creating the ActionScript

  1. Select: Frame 1
  2. Open the Actions Panel: Window > Actions (F9)
  3. Important Note: Make sure that you can see Actions - Frame at the top of the Actions Panel:

    The Actions Panel has Actions - Frame in the top corner.

  4. In the Actions Panel if Script Assist is on, Switch: Script Assist Off
  5. Note: To see if Script Assist is on or off go to the Action Panel's Side Menu Button:   

    Script Assist is On when ticked: (remove the tick)

    Cross Ref: If you are not sure what Script Assist is or how to switch it on and off see the short Tutorial: Actions Panel

  6. Type (or Copy & Paste) the following Actionscript into Actions Panel:
  7. // Sets the number of images in the random sequence
    // IMPORTANT: Set this number to how many images you have

    var myImageSequence = 25;

    // Creates a random number:
    var myRandomNumber = Math.floor(Math.random()*myImageSequence)+1;

    // Creates the full image name like: 1.jpg
    // IMPORTANT: If you're NOT using Jpegs change jpg to png or gif
    var myImageName = myRandomNumber + ".jpg";

    // Loads the random image:
    loadMovieNum(myImageName, 1);

  8. Change  myImageSequence to the number of images in your sequence. Mine is: 25
  9. If you're not using Jpegs change the code above from: ".jpg" to ".png" or ".gif"
  10. Note: If your images are not the same size as your Flash Movie then the code above will align your images to the top left. If you would rather that your images were aligned to the center then change the load Movie Number from 1 to 0 like this:

    (myImageName, 0);

  11. Test your movie: Control > Test Movie (Ctrl Enter)

    Note: This will load one image only. Just to check that the random load is working test your Movie several times in a row by repeatedly pressing: Ctrl Enter (You may see a blank Movie for a short while when waiting for the next image to load, this is just like refreshing you browser and is normal. If you don't wish to wait press Ctrl Enter twice in a row).

    Important: In test mode if you have not saved your Flash file into the same folder as your images your movie will not work. When you upload your files to the Internet then the HTML page must be in the same location as the images and the location of the Flash Movie becomes irrelevant. If you want to use your Flash file in multiple web pages and load images form one location you will need to specify an absolute address like this:

    var myImageName = "" + myRandomNumber + ".jpg";

I hope you have found this useful. If so perhaps you could recommend this site to others and link to webwasp!


Please indicate what you thought of this tutorial 
10 is the best: 
10 9 8 7 6 5 4 3 2 1


    << Previous    1  2  3  4  5   Next >>     

49824 visitors to this page since 26 Oct 07 •

Phil Schulz's Facebook Profile
Webwasp is Phil Schulz's baby. You are welcome to contact me or become my Facebook friend: Click here

 Top of Page HomeMembers Tutorials Forum iSnapChat Contact Us 
 All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.