Flash ActionScript 3 Tutorial:
Load Random MovieClip from Library to an Empty MC
This tutorial shows how to load a random MovieClip from the Library to an Empty MovieClip on the Stage with Flash ActionScript.
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") {
myMovieClip = new Tree();
output_txt.text = "The Tree MovieClip was added!";
} else if ((movieArray[Math.floor(Math.random() * 3)]) == "Sun") {
myMovieClip = new Sun();
output_txt.text = "The Sun MovieClip was added!";
} else if ((movieArray[Math.floor(Math.random() * 3)]) == "FootBall") {
myMovieClip = new FootBall();
output_txt.text = "The FootBall MovieClip was added!";
} else { // In case the random number is 1
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:
Remarks:
This Flash ActionScript tutorial shows how to load a random MovieClip from the Library to an empty MovieClip on the stage.