Macromedia Flash 8 Bible Free Open Book

Macromedia Flash 8 Bible

Previous Page
Next Page

Functions as Constructors for Objects

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.

On the CD-ROM 

Make a local copy of the soundObjects.fla file, located in the ch26 folder of this book's CD-ROM.

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 Definition

The 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 Assignment

After 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 Execution

Finally, 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.

Caution 

The setVolume() method of the Sound object controls all Sound objects linked to the same target timeline. This means that each Sound object must target a unique MovieClip object, in the new Sound() constructor. For example, if you wanted to separately control the volume of five individual sounds, make sure you use a different target MovieClip instance for each of those Sound objects. To safeguard for this condition, we used the createEmptyMovieClip() method in our example to allocate sound resources to a separate timeline, designated by the local variable target in the SoundLibrary() function.

Converting the Function Definition to a Class Definition

You'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.

Note 

You need to be using Flash Professional 8 to complete the following steps.

  1. Create a new ActionScript document in Flash Pro 8 by choosing File ð New, selecting ActionScript File in the General tab of the New Document dialog box (see Figure 26-5), and clicking OK.

  2. Save the new document as SoundLibrary.as, in the same folder as the soundObjects.fla file you copied from the CD-ROM.

    Caution 

    It's necessary to name the .as file the same name as the class you are going to define within the file. The filename is case-sensitive, and the .as file needs to be in the same folder as the .fla file in order for this example to work.

  3. Go back to the soundObjects.fla file, and resave it as soundObjects_AS2.fla.

  4. Select frame 1 of the actions layer in the soundObjects_AS2.fla document. Cut the entire SoundLibrary function by selecting the function code and pressing Ctrl+X or z+X. You should only have three lines of ActionScript code left in your Actions panel.

  5. Switch to the SoundLibrary.as file, and paste the code into the document (Ctrl+V or z+V).

  6. Add the following code around the function code you just pasted. Where you see the [existing function code], you should have the original code from the soundObjects.fla document:

    class SoundLibrary {
       [existing function code]
    }
    
  7. In order for the SoundLibrary class to work, you need to expose the snd array. Modify the code with the bold lines shown below:

    class SoundLibrary {
    
           public var snd:Array;
    
           function SoundLibrary(begin:Number, end:Number) {
    
                  snd = new Array();
                  if(_root.soundLib == undefined){ ...
    

    This modification simply removes the this reference from the snd array created within the constructor and removes its declaration from within the function because snd is declared below the class definition.

  8. Save the SoundLibrary.as file.

  9. Switch back to the soundObjects_AS2.fla document, and select frame 1 of the actions layer. Open the Actions panel, and modify the script to the following code:

    import SoundLibrary;
    
    var soundLib:SoundLibrary = new SoundLibrary(1, 7);
    soundLib.snd[1].start();
    soundLib.snd[2].start();
    

    Here, you use the import keyword to let the ActionScript compiler know that you want to use the SoundLibrary.as class file. You don't specify the .as file extension when you use the import keyword.

  10. Save your document, and test it (Ctrl+Enter or z+Enter). You should hear the two sounds play, just as you did with the original example.

Image from book
Figure 26-5: The New Document dialog box
On the CD-ROM 

You can find the completed files, soundObject_AS2.fla and SoundLibrary.as, in the ch26 folder of this book's CD-ROM.

Wed Resource 

We'd like to know what you think about this chapter. Visit www.flashsupport.com/feedback to send us your comments.


Previous Page
Next Page
Index: [A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y][Z]


     Main Menu
Table of Contents
Back Cover
Macromedia Flash 8 Bible
Foreword
Preface
Part I: An Introduction to Flash Web Production
Part II: Mastering the Flash Environment
Part III: Creating Animation and Effects
Part IV: Integrating Media Files with Flash
Part V: Adding Basic Interactivity to Flash Movies
Part VI: Distributing Flash Movies
Part VII: Approaching ActionScript
Chapter 24: Knowing the Nuts and Bolts of Code
Chapter 25: Controlling Movie Clips
Chapter 26: Using Functions and Arrays
What are Data Types?
Overview of Functions as Procedures
Managing Related Data: The Array Class
Creating a Dynamic Reusable Flash Menu
Functions as Methods of Objects
Functions as Constructors for Objects
Summary
Chapter 27: Interacting with Movie Clips
Part VIII: Applying ActionScript
Part IX: Integrating Components and Data-Binding
Part X: Expanding Flash
Part XI: Appendixes
Index
List of Figures
List of Tables
List of Listings
List of Sidebars


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele