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 Random MovieClip from Library to Empty MC AS 3 Tutorial

This tutorial shows how to load a random MovieClip from the Library to an Empty MovieClip on the Stage with Flash ActionScript.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

Sometimes you may have more than one MovieClips in the Flash Library. This Flash tutorial shows how to load a random MovieClip from Library to an Empty MovieClip on the Stage with Flash ActionScript.

Flash ActionScript Codes:

/*
Preparation: Create Linkage for the MovieClip

 

STEP 1: Prepare MovieClips
You already prepared three MovieClips in the library
In this example, the name of the MovieClips are:
- Tree
- Sun
- FootBall

 

STEP 2: Create Linkage
Select the movieclip from the library window
Select "Linkage..." from the pop up window
The Linkage Properties window appear
Check the "Export for ActionScript"
Enter Name in the Class field
In this example, we used "Tree", "Sun" and "FootBall" for the class name

 

Preparation: Create an empty MovieClip

 

1. Select "Insert" from the menu.
2. Select "New Symbol" from the drop down menu
3. The "Create New Symbol" window pop up.
4. Enter "emptyMC" in the Name field. Select MovieClip.
5. Select "Scene"
6. Drag the "emptyMC" to a desired position on the stage (e.g. 150, 210)
7. Give a new instance name "emptyMC_mc" to this empty MovieClip
*/


//Create an empty Array with the name movieArray
var movieArray:Array = new Array();


//Put the MovieClips into the Array
//The size of this Array is now 3
movieArray = ["Tree", "Sun", "FootBall"];

 

// Declare variable to use later
var myMovieClip:MovieClip;

if ((movieArray[Math.floor(Math.random() * 3)]) == "Tree") {

// Create a new MovieClip
myMovieClip = new Tree();
output_txt.text = "The Tree MovieClip was added!";

} else if ((movieArray[Math.floor(Math.random() * 3)]) == "Sun") {

// Create a new MovieClip
myMovieClip = new Sun();
output_txt.text = "The Sun MovieClip was added!";

} else if ((movieArray[Math.floor(Math.random() * 3)]) == "FootBall") {

// Create a new MovieClip
myMovieClip = new FootBall();
output_txt.text = "The FootBall MovieClip was added!";

} else { // In case the random number is 1

myMovieClip = new FootBall();
output_txt.text = "The FootBall MovieClip was added!";

}

 

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

 

// Set the location if required
//emptyMC_mc.x = 250;
//emptyMC_mc.y = 210;

Download Flash Source File:

Flash Source File load-mc-3.fla

Remarks:

This Flash ActionScript tutorial shows how to load a random MovieClip from the Library to an empty MovieClip on the stage.