Loading Random Movies
      by kirupa - 25 January 2004

Now that you have built-up some confidence knowing that it is possible to create randomly loading movies from the previous page, it is time for you to understand why the random movie code works.

Let's dissect each line:

filename = ["circular.swf", "vibration.swf", "random_movement.swf"];

The above line gives the filename of each external movie I would like to load. As you can see, I have three SWFs that I would like to load and have a chance of being displayed.

path = "//www.kirupa.com/developer/actionscript/animation/";

In this line I specify the location of the folder the external movies will be loaded into. This line is optional, for if you want, you can specify the location directly in the filename itself. I split the path and filename because it makes reading the code a little easier.

i = filename.length;

The variable i returns a number equal to the number minus one of items in the filename array. You should know that in arrays, numbering begins with 0 as opposed to 1. You may want to check out the Arrays tutorial for more information.

k = Math.floor(Math.random()*i);

The variable k creates a random number using the Math.random() function and the number from the variable i declared in the previous line. I use Math.floor to get an integer that is less than or equal to the number the function generates.

loadMovie(path+filename[k], movieTarget);

Here is where all of the efforts of the previous lines of code come together. The loadMovie function basically works with two arguments - the filename and the target location the movie will be loaded.

In our case, path+filename[k] give a full URL of the movie that is going to be loaded. How does it work? Well - filename[k] is related to arrays. k is an integer value between 0 and i. In our case, k can be any integer between 0 and 2. For example, filename[k] will return the 3 item from the filename array: random_movement.swf.

So, when the variables path+filename[k] come together, you get the full URL to your movie. Finally, movieTarget, if you remember, is the instance name you gave to the circle movie clip in the previous page.

If you want to port this code to your own animations, simply replace the name of the filenames in the filename variable to that of movies that you would like to load. Also, make sure to update the path variable with the path to the folder on your site that contains your SWFs. For example, if all of the animations you would like to load are in the folder //www.kirupa.com/swf/ simply set the aforementioned URL into your path.

Finally, make sure that the target in your loadMovie function refers to the instance name of the movie clip that the external SWFs will load into. The rest of the code takes care of all the details automatically. The number of movies you specify, etc. is all taken care of. So, relax, lean back, drink a few glasses of a cold non-alcoholic beverage, and put on your favorite beach screensaver. This code not only loads random movies but also is a great stress reliever!

In the first line you specify the names of the movie clips you would like to use in your animation. You place it in an array format because it makes it easier for you to pick one file from the list of movies. You have a counter variable i that keeps track of how many items you have added to your array. That frees your hands from having to manually enter the number of movie clips later in the Math.random() function.

The loadMovie code combines the text from path and the text from filename[k]. For example, if k happened to be 1, Flash would interpret the loadMovie line to look like the following line:

vibration.swf, movieTarget);

Pretty nifty ehh? That does it for this tutorial. I have provided the source code in MX form, for the code for MX2004 is exactly the same:

Download Source

Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!

Kirupa's signature!




kirupa.com's fast and reliable hosting provided by Media Temple.