Home > Articles > Computer Software > Creative Software > Other

  • Print
  • + Share This
  • 💬 Discuss
This chapter is from the book

Lesson 19: Duplicating Movie Clips

Being able to manipulate movie clips is an important part of building games. However, you'll also need to be able to create movie clips. Although this is easy enough to do in Flash, you might want your code to be able to create the movie clips after the movie has started running.

Think about a game in which enemy spaceships are attacking the player. One ship comes, and then another, and then another, and so on. Instead of having to create dozens or hundreds of movie clips ready for the game to use, you can have the code create them as necessary.

There are two ways to create movie clips. One is to duplicate a movie clip that already exists. Another is to create a movie clip from a symbol in the library.

To duplicate a movie clip, you need to use the duplicateMovieClip command. This command creates a carbon copy of an existing movie clip. Here is an example:

firstclip.duplicateMovieClip("newclip",0);

The command duplicateMovieClip must be triggered by the movie clip to be duplicated. So it starts with the name of that movie clip, in this case "firstclip." There are also two parameters. The first parameter is the name of the new movie clip instance. The second parameter is the level of this new movie clip.

This is where it can get a bit confusing. The duplicateMovieClip command uses the term "level" to describe the order in which movie clips are drawn. This is different than the use of the term "level" in the last section, when it was used to describe which movie clips were inside other movie clips.

If a movie clip is drawn at level 0, like in the previous one-line code example, then it is drawn under a movie clip at level 1. Level 1 is drawn under level 2, and so on.

You don't have to worry about two movie clips being at the same level because they can't be. Flash won't let two movie clips be created at the same level. So with every use of duplicateMovieClip, you need to use a different level number.

Example file: DuplicateMovieClip.fla

In DuplicateMovieClip.fla, the first and only frame of the main timeline contains the variable "level," which is set to 0. On the Stage is a button labeled "New MC." There is also a movie clip with the name "firstclip." Each time the button is pressed, the following code is executed:

on (press) {
  // duplicate first clip
  firstclip.duplicateMovieClip("newclip"+level,level);

  // set a random position
  _root["newclip"+level]._x = int(Math.random()*550);
  _root["newclip"+level]._y = int(Math.random()*400);

  // increase level
  level++;
}

NOTE

Notice that for the first time, I've used comments in the ActionScript code. These comments start with two slashes. Everything else on a line after the two slashes is ignored by Flash and is there only to make it easier for you to understand what the code is doing. As the blocks of code in this book get longer and longer, you'll see comments to help you read through them. You should also use comments in your code to make it easier to edit later, or for a co-worker to be able to understand it more easily.

The variable "level" is used in many ways in this handler. First, it is used to compose a name for the new movie clip. The first time the variable is used, the movie clip will be named "newclip0." The variable is also used as the level for the movie clip.

At the end of the handler, "level" is incremented by one. So the next movie clip created will be "level1" at level number 1.

This handler also sets the horizontal and vertical location of this new movie clip to a random spot on the Stage. Notice that the _root[] syntax is used to indicate which movie clip is being referred to.

Another way to create new movie clips is to use the attachMovie command. This command does not require that the movie clip already be on the Stage. Instead, it just needs to be in the library. However, if a movie clip is in the library and not used on the Stage, Flash automatically won't include it in the final .swf file. To force it to include this movie clip, you need to select it in the library and use the Options menu to set the Symbol Linkage Properties. You can see this dialog box in Figure 3.5.

Figure 3.5Figure 3.5 The Symbol Linkage Properties dialog box lets you include a movie clip in the .swf even if it isn't used on the Stage.

The Symbol Linkage Properties needs to be set to Export This Symbol. Then, you have to come up with a name for the symbol that the code will use to refer to it. I usually use the symbol's name as it appears in the library.

Example file: AttachMovie.fla

The attachMovie command uses the name in the Symbol Linkage Properties dialog box as the first parameter. The second parameter is the name of the instance of the movie clip on the Stage, and the third parameter is its level. The code for this button, found in the movie AttachMovie.fla, is almost the same as the code for the DuplicateMovieClip.fla movie, with the exception of one line.

on (press) {

  // duplicate first clip
  attachMovie("myMovieClip","newclip"+level,level);

  // set a random position
  _root["newclip"+level]._x = int(Math.random()*550);
  _root["newclip"+level]._y = int(Math.random()*400);

  // increase level
  level++;
}

Example file: RemoveMovieClip.fla

You can also remove movie clips from the Stage by using the removeMovieClip command. For instance, the following code, found in RemoveMovieClip.fla, removes the previous movie clip before creating a new one:

on (press) {
  // remove previous movie clip
  _root["newclip"+(level-1)].removeMovieClip();

  // duplicate first clip
  attachMovie("myMovieClip","newclip"+level,level);

  // set a random position
  _root["newclip"+level]._x = int(Math.random()*550);
  _root["newclip"+level]._y = int(Math.random()*400);

  // increase level
  level++;
}

These techniques mean that your games and applications can create their own movie clips, adding them and removing them from the Stage as necessary.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus