|
Free Open Book
Macromedia Flash 8 Bible |
Functions as Constructors for ObjectsFunctions can also be used with the new constructor to create objects with properties and methods assigned by the function. This means that you can use a function to create unique objects, based on parameters that you pass as arguments to the function upon invocation. In this section, we deconstruct another function example that creates an entire sound library with ActionScript, without using any Movie Clip instances.
Open your copy of the soundObjects.fla file in Flash 8. You'll notice that there aren't any Movie Clips and/or physical elements on the Stage. Select the first (and only) frame on the actions layer. Open the Actions panel, and you'll see the following code:
function SoundLibrary(begin:Number, end:Number) {
var snd:Array = this.snd = new Array();
if(_root.soundLib == undefined){
var mcLib:MovieClip = _root.createEmptyMovieClip(
"soundLib", _root.getNextHighestDepth());
} else {
var mcLib:MovieClip = _root.soundLib;
}
for (var i:Number = begin; i <= end; i++) {
var nDepth:Number = mcLib.getNextHighestDepth();
var target:MovieClip = mcLib.createEmptyMovieClip("snd_" +nDepth,
nDepth);
snd[i] = new Sound(target);
snd[i].attachSound("sound_" + i);
}
}
var soundLib = new SoundLibrary(1, 7);
soundLib.snd[1].start();
soundLib.snd[2].start();
There are three sections to this code: the function definition; the object creation and assignment; and the method execution of the Sound objects. Function DefinitionThe SoundLibrary() function has two arguments: begin and end. Again, these are user-defined function names and arguments. You could rename the function and arguments to your own preferred terms. The for loop in the SoundLibrary() function will create a snd array object within the calling object (this). This array will contain Sound objects that use the sound files in the Library. Note that each of the sounds in the Library have been set to export with the Flash .swf file, as defined by the Linkage Properties for each sound. Object Creation and AssignmentAfter the SoundLibrary() function is defined, you can use it for new objects. In this example, a new object named soundLib is created after the function definition: var soundLib = new SoundLibrary(1,7); First, the object is declared as being on the current timeline (this or _root). By not specifying _root directly, you can load this Flash movie into MovieClip objects in other Flash movies and retain proper targeting paths for the createLib() function. If you test this movie on its own, soundLib will be declared on _root or _level0. Using the new constructor, we create the snd array and Sound objects relative to the soundLib object. We are creating a unique object with specific properties and values. This enables you to make as many objects as you desire, all from one function: var soundLib_1 = new SoundLibrary(1,3); var soundLib_2 = new SoundLibrary(4,7); These actions (not used in our example) would create two separate soundLib objects, each using a specific range of Sound objects that play linked sound files from the Library. The numbers specified in the parentheses indicate the sounds to use from the Library. Remember that in our function SoundLibrary(), the begin and end arguments are used to form the linkage identifiers: "sound_" + i where i is defined by the begin argument, and incremented until the end argument value is reached. Sound Object Method ExecutionFinally, after the Sound objects are created within the soundLib object, you can play the sounds with the built-in ActionScript start() method for Sound objects: this.soundLib.snd[1].start(); this.soundLib.snd[2].start(); These lines of code tell the Sound objects located in the 1 and 2 index positions of the snd array (in this example, sound_1 and sound_2 from the Library) to play. This is just one way of using functions to create new objects. You can use functions to create other types of data-based objects for record storage and retrieval.
Converting the Function Definition to a Class DefinitionYou're actually only a few steps away from creating your first ActionScript 2.0 class. Now that you understand how you can use a function as a constructor for a new object type (in this case, SoundLibrary), you can easily convert your SoundLibrary function into a SoundLibrary class.
|
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |