Functions as Constructors for Objects Flash

Functions 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 5. You’ll notice that there aren’t any Movie Clips and/or physical elements on the Stage. Double-click the first (and only) frame on the actions layer. In the Actions Panel, you’ll see the following code:

There are three sections to this code: the function definition; the object creation and assignment; and the method execution of the Sound Objects.

Function definition
The createLib function has two arguments: start and end. Again, these are userdefined function names and arguments. You could rename the function and arguments to your own preferred terms. The for loop in the createLib function will create a snd array object within the calling object (this). This array will contain Sound Objects that use the .AIFF sound files in the Library. Note that each of the sounds in the Library have been set to export with the .SWF file, as defined by the Linkage Properties for each sound.

Object creation and assignment
After the createLib function is defined, we can use it for new objects. In our example, a new object named soundLib is created after the function definition:

this.soundLib = new createLib(1,7);

First, the object is declared as being on the this timeline. This enables us to load this Flash movie into other Flash movies and retain proper targeting paths for the createLib function. If you test this movie on its own, this will simply be equal to _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:

These actions (not used in our example) would create two separate soundLib objects, each using a specific range of Sound Objects from the Library.

The numbers specified in the parentheses indicate the sounds to use from the Library. Remember that in our function createLib, the start and end arguments are used to form the linkage identifiers:


where i is defined by the start argument, and incremented until the end argument value is reached.

Sound Object method execution
Finally, after the Sound Objects are created within the soundLib object, we can play the sounds with the Flash 5 built-in start method for Sound Objects:

These lines of code tell the Sound Objects located in the 1 and 2 index positions of the snd array (for example, sound_1 and sound_2 from the Library) to play. This is just one example of using functions to create new objects. You can use functions to create other types of data-based objects for record storage and retrieval, as well as to create unique Color Objects to reference with more than one Movie Clip target.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Flash Topics