Adobe Flash

Play a movie clip

You can configure your document to play a movie clip at runtime. Using the attachMovie() method, you can attach an instance of a movie clip in the Library panel to the Stage even though you have not placed an instance on the Stage.

With the attachMovie() method, you must export the symbol for ActionScript and assign it a unique linkage identifier, which is different from the instance name.

1.
In the Library panel, right-click the MCTrio symbol and select Linkage from the context menu.

2.
In the Linkage Properties dialog box, select Export for ActionScript.

3.
In the Identifier text box, verify that MCTrio appears as the linkage name.

4.
Verify that Export in First Frame is selected, and then click OK.

Movie clips that are exported for use with ActionScript load, by default, before the first frame of the SWF file that contains them. This can create a delay before the first frame plays. When you assign a linkage identifier to an element, you can specify that the movie clip loads on the first frame to avoid playback delays.

Use the attachMovie() method to play a movie clip

You'll now use the attachMovie() method to load the movie clip and provide the symbol with an instance name. Since the instance of the symbol does not exist on the Stage, you'll create the instance name programmatically.

1.
In the Timeline, select Frame 1 of the Actions layer for Scene 1.

2.
In the Script pane of the Actions panel, place the insertion point at the end of your last line of code.

Press Enter (Windows) or Return (Macintosh), and then type the following to add a comment and create a new function:

// function plays trio_mc when attachMovie_btn instance

// is released

attachMovie_btn.onRelease = function(){

Next, you'll specify what the function does: it plays the movie clip on the root Timeline, which is the main timeline. In your script, you'll refer to the movie clip by the linkage identifier name in the Linkage Properties dialog box (MCTrio).

Additionally, even though you didn't place an instance of the MCTrio symbol on the Stage, you'll use ActionScript to create an instance name for the symbol. The instance name that you'll specify is trio_mc.

3.
With the insertion point at the end of the last line of script, press Enter or Return. Then type the following:

this._parent.attachMovie("MCTrio", "trio_mc", 1);

The number 1 in the script that you just typed refers to the depth on the layer in which to play the movie clip.

Every movie clip instance has its own z axis (depth) that determines the stacking order of a movie clip within its parent SWF file or movie clip. When you use the attachMovie() method to create a new movie clip at runtime, you always specify a depth for the new clip as a method parameter.

Note

For more information about the attachMovie() method, see attachMovie() in the ActionScript 2.0 Language Reference. Additionally, you can use the ActionScript 2.0 Language Reference for information about ActionScript that allows you to manage depth; getNextHighestDepth(), getDepth(), getInstanceAtDepth() are methods of the MovieClip class. The DepthManager class allows you to manage the relative depth assignments of a movie clip.

Specify movie clip Stage coordinates

In addition to the z axis for the movie clip, you must specify the x and y coordinates to place the movie clip within the Stage area at runtime.

  • Press Enter (Windows) or Return (Macintosh) after the last line in the Script pane and type the following:

    trio_mc._x = 275;
    
    trio_mc._y = 200;
    
    };
    

Unload the movie clip

After the movie clip plays, you need a way to remove the movie clip from the Stage when the user goes to Scene 2. You can modify your script for the goScene_btn to "unload" the movie clip.

1.
In the Timeline, select Frame 1 of the Actions layer.

Then click at the end of the following line of script in the Script pane, within the function that takes the user to Scene 2, to place the insertion point:

  gotoAndStop("Scene 2", 1);

2.
Press Enter (Windows) or Return (Macintosh) and type the following script, which unloads the movie clip when the function runs, so that the movie clip does not continue to play when the user goes to Scene 2:

unloadMovie("trio_mc");

Your entire function for the goScene_btn should appear as follows:

// This script takes user to Scene 2 when goScene_btn

// instance is released.

goScene_btn.onRelease = function() {

  gotoAndStop("Scene 2", 1);

  unloadMovie("trio_mc");

};

Your entire script should appears as follows:

// Stops the playhead at Frame 1.

stop();



// This script takes user to Scene 2 when goScene_btn

// instance is released.

goScene_btn.onRelease = function (){

  gotoAndStop("Scene 2", 1);

  unloadMovie("trio_mc");

};



// This function plays trio_mc when attachMovie_btn

// instance is released.

attachMovie_btn.onRelease = function(){

this._parent.attachMovie("MCTrio", "trio_mc", 1);

trio_mc._x = 275;

trio_mc._y = 200;

by BrainBellupdated
Advertisement: