|[ LiB ]|
One problem with duplicating an existing Movie Clip is that you have to find something to do with the original clip. Sometimes you don't want an original clip on the stage, and sometimes you might just want to add and remove Movie Clips and not worry about strays in the way. Well, you're in luckActionScript allows you to do all of this.
Take a look at the usage of a method called attachMovie , which is part of the MovieClip class (just like duplicateMovieClip is):
myMovieClip.attachMovie( "idName", "newClipInstance", depth );
It works in very much the same way duplicateMovieClip does, except that attachMovie creates instances of a symbol in the Library window instead of a copy of another instance on the stage. A big difference is that you can copy the code that's embedded in your duplicated Movie Clips. This isn't possible when attaching Movie Clips from the Library.
Let's skip the explanation of the idName parameter for nowI'll just tell you that this is the linkage name for the symbol in the Library.
The newClipInstance parameter is very simplejust like in the duplicateMovieClip method, this will be the instance name of the new clip. The depth parameter defines what internal layer the Movie Clip will be assigned to. Remember that all attached or duplicated Movie Clips should be on unique layers .
Remember that newClipInstance and depth should always be unique.
In order to better explain the idName parameter, I have written another demo for you. It is named GDA_PROG6.3.fla. Go ahead and open it and navigate to the second frame. Notice that there aren't any objects on the stage except for the background graphics. See Figure 6.3.
Now go ahead and test the movie. Click the button and you'll have a whole bunch of marbles bouncing all over the screen. Nice trick, eh? Take a look at Figure 6.4 for the "after" shots.
Go back to the project file and access your Library window. (In case you forgot how to do that, press F11.) Once you have the Library window open, stretch it until you see all the fields shown in Figure 6.5.
Look at the fourth column under Linkage. Notice that the Movie Clip named "ball" has a statement under the linkage column Export: ball. "ball" happens to be the name attachMovie will use to create instances of this clip.
So what am I making you do here? What you are essentially doing is learning and familiarizing yourself with assigning linkage properties to your Movie Clips so that you can create instances of them in ActionScript.That's all. It sounds complicated, but all you're doing is assigning the symbol an alias that you can use to refer to the symbol in ActionScript.
So now that you know how to check what the linkage name is for a symbol, how would you set one up? Let's look into what you already have in front of you. In the Library window, right-click or Control-click (Mac users) on your ball symbol. From the context menu, select Linkage. The Linkage Properties dialog box, shown in Figure 6.6, will open.
Let me quickly tell you a little about the internals of how Flash writes an SWF file. Flash first examines everything in the library. It makes sure that all the symbols are being used. If they're not, the symbols are not exported with the file. But sometimes when writing a game, you do not want symbols on your Timeline because you want to create dynamic Movie Clips that are created on-the-fly .
As you are not duplicating a clip that's already on the stage, attachMovie will not copy code into the instancesit will only copy the graphics from the symbol.
So how do you get around this if Flash doesn't export the Movie Clips that aren't being used? This is when you set up the symbols linkage properties from your Library windowin the same Linkage Properties dialog box you see in Figure 6.6.
As you probably already guessed, you can remove clips dynamically. You can either use the action or the method way of the removeMovieClip command. If you use it as a Movie Clip method (with the dot notation) all you have to do is call this function and your clip disappears. If you use it as an action, all you have to do is call this function with the name of the target clip within its parentheses. If you want more information on this action command, don't hesitate to look it up in the Help file.
By setting up the linkage properties, you can let Flash know that you want your symbol included in the first frame. This guarantees that your symbol will be included with the project. You can also assign an alias that you can use from within ActionScript with the attachMovie command.
To ensure that ActionScript assigned this exported name, make sure the Export for ActionScript option is selected. To ensure that the symbol is exported with the SWF movie file, select the Export in first frame option. Click OK after you've assigned an ID name and you should be ready to create instances of that symbol dynamically.
|[ LiB ]|