Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

Load Different MovieClip from Library to Empty MovieClip in Random AS 3 Tutorial

In previous two flash ActionScript tutorial, we show how to load different MovieClips from the Library to an Empty MovieClip on the Stage one by one with loop animation. In this Flash AS tutorial, we try to load MovieClip from the library on the stage in a random order.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

In this Flash ActionScript tutorial, we add a more interesting effect. The MovieClips are loaded from the Library in random. This method is almost same as the previous tutorial. The Flash ActionScript codes in frame 1 and frame 30 can be used for all MovieClip. The ActionScript in frame 1 will load the MovieClips from the Library according to the generated random number, while the AS codes in frame 30 will remove the loaded MovieClip.

set Linkage of MovieClip

The completed Flash Movie of this tutorial is shown as above.

Flash ActionScript Codes (Frame 1):

// function to get a random number within a range
function getRandom(_min, _max):Number{
var tNumber:Number;
tNumber = Math.round(Math.random()*(_max-_min))+_min;
return tNumber;
}

// get a random number between 1 and 3
var myNumber:Number = getRandom(1, 3);

// declare a new MovieClip
var myMovieClip:MovieClip;

// Load MovieClip from Library depends on the generated random number
if (myNumber == 1) {

output_txt.text = "The Tree MovieClip was added!";

// Create a new MovieClip
myMovieClip = new Tree();

// Add the new MovieClip to the empty MovieClip
// so that we can see it.
emptyMC_mc.addChild(myMovieClip);

} else if (myNumber == 2) {

output_txt.text = "The Light House MovieClip was added!";

// Create a new MovieClip
myMovieClip = new light_house();

// Add the new MovieClip to the empty MovieClip
// so that we can see it.
emptyMC_mc.addChild(myMovieClip);

 

} else if (myNumber == 3) {

output_txt.text = "The Plane MovieClip was added!";

// Create a new MovieClip
myMovieClip = new Plane();

// Add the new MovieClip to the empty MovieClip
// so that we can see it.
emptyMC_mc.addChild(myMovieClip);

}

Actually the above codes are similar to the previous tutorial except that the MovieClips from the Library are loaded on the stage according to the generated random number.

Then the loaded MovieClip was removed in frame 30 using the following Flash ActionScript codes:

// Remove the loaded MovieClip
emptyMC_mc.removeChild(myMovieClip);

Download Flash Source File:

Flash Source File load-mc-6.fla

Remarks:

This Flash ActionScript tutorial shows how to load different MovieClip from the library to an empty Movieclip on the stage in random order.