Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

 

 


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 = "http://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.


Application
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 http://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!

Summary
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:

loadMovie(http://www.kirupa.com/developer/actionscript/animation/
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

I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 
Cheers!
Kirupa Chinnathambi
kirupaBlog

 


 

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components

Flash Templates
CSS Templates
Dreamweaver Templates

flash menus, buttons and components
Digicrafts Components The best flash components ever!
Entheos Flash Website Templates Buy and sell FLAs at Ultrashock!
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Purchase & Download Flash Components
flash components  Learn how to advertise on kirupa.com