Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

macromedia flash mx - Reinhardt R.

Reinhardt R., Lott J macromedia flash mx - John Wiley & Sons, 2004. - 987 p.
ISBN 0-7645-4354-7
Download (direct link): macromediaflash2004.pdf
Previous << 1 .. 120 121 122 123 124 125 < 126 > 127 128 129 130 131 132 .. 419 >> Next

As with the duplicateMovieClip() method, the attachMovie() method returns a reference to the newly created instance. And, as with duplicateMovieClip(), this can be helpful when you would otherwise have to type long or awkward target paths.
Working with Init Objects
When you are creating new instances using duplicateMovieClip() and/or attachMovie(), often you want to initialize the new object with certain values. For example, you might want to assign each instance x and y coordinate values so that the instances donít overlap (with duplicateMovieClip()) or so that the instance does not appear at 0,0 (with attachMovieClip()). As youíve already seen, it is possible to set these values immediately after creating the new instance. But ActionScript also includes an option by which you can assign these property values when creating the new object. In order to accomplish this both the duplicateMovieClip() and the attachMovieClip() methods accept an additional, optional parameter called an init object.
An init object is an Object i nstance to which you have assigned properties and values that correspond to the properties for the new MovieClip instance you want to create. For example, you can create a new init object with _x and _y properties. Flash will then automatically assign the values from the init object to the corresponding properties in the new MovieClip instance. You can create the init object using an Object constructor. However, in many cases you may find it more convenient to use object literal notation to define the init object inline within the attachMovie() or duplicateMovieClip() method. Letís take a look at an example. In the following code, a new MovieClip i nstance is attached using attachMovie(). The first three parameters remain as before ó the linkage identifier, the new instance name, and the depth. Additionally, an init object is defined with _x and _y properties.
mcHolder.attachMovie("CircleSymbol",
"mcCircle",
mcHolder.getNextHighestDepth(),
{_x: 50, _y: 60});
242
Part III ¶ MovieClip, Button, and Drawing API
In the preceding example the new instance is initialized with an x coordinate of 50 and a y coordinate of 60.
Likewise, you can utilize an init object with duplicateMovieClip(). Hereís an example:
mcCircle.duplicateMovieClip("mcNewCircle",
this.getNextHighestDepth(),
{_x: 50, _y: 60});
If you recall, we said earlier that when you create a duplicate MovieClip using duplicateMovieClip() none of the custom properties of the original are copied to the duplicate. However, with the init object you can conveniently copy all those properties to the duplicate. As mentioned previously, all classes extend, either directly or indirectly, the Object class. Therefore, any type of object, including a MovieClip object, can serve as an init object. With this in mind, if you want a duplicated MovieClip to contain all the same custom properties as the original, you can use the original object as the init object. Here is an example:
mcCircle.radius = 100;
mcCircle.duplicateMovieClip("mcNewCircle",
this.getNextHighestDepth(),
mcCircle);
trace(mcNewCircle.radius); // Displays: 100
Creating Empty MovieClip Objects
Why in the world would you want to create an empty MovieClip instance? Simple: Because you can. Okay, and also because it can be extraordinarily beneficial. Although an empty MovieClip object may not immediately appear useful, it is exactly its emptiness that makes it such a powerful object. Here are two of the most common uses of empty MovieClip objects:
¶ Attaching several MovieClip instances within the empty MovieClip object. By nesting the objects within the single parent it makes it simple to move, resize, or otherwise alter the entire group.
¶ Loading content. As youíll see shortly, you can load other SWF files or JPEG files from external sources into a MovieClip instance. It is very useful to be able to programmatically create an empty MovieClip for this purpose.
You can create a new empty MovieClip instance with the createEmptyMovieClip() method. This method creates a new instance nested within the MovieClip from which it is called. The method requires two parameters ó the name and depth for the new instance. Here is an example:
this.createEmptyMovieClip("mcExternalSWFHolder", this.getNextHighestDepth());
The createEmptyMovieClip() method creates the new instance at 0,0 within the coordinate space of the parent object. Unlike duplicateMovieClip() and attachMovie(), the createEmptyMovieClip() method does not accept an init object. If you want to change the placement of the new object you have to set the _x and _y properties of the new instance after it has been created. Also, like duplicateMovieClip() and attachMovie(), the createEmptyMovieClip() method returns a reference to the new instance.
Chapter 9 ¶ MovieClip and Button Classes
243
Removing Programmatically Generated MovieClip Objects
Once youíve added MovieClip instances programmatically with duplicateMovieClip(), attachMovie(), or createEmptyMovieClip() you can work with those MovieClip objects in the same way as any other MovieClip objects. You can read and write properties to set placement, visibility, alpha, and so on. You can invoke the methods to affect timeline playback, change depths, and so on. But in addition to all that, you can also do one thing that you cannot do with an authoring time MovieClip instance ó you can remove them. The removeMovieClip() method, when invoked from a programmatically generated MovieClip object will remove the object from the stage. This method works only for MovieClip objects created using one of the three aforementioned methods. If you invoke removeMovieClip() on an authoring time clip there is no effect.
Previous << 1 .. 120 121 122 123 124 125 < 126 > 127 128 129 130 131 132 .. 419 >> Next