Macromedia Flash 8 Bible Free Open Book

Macromedia Flash 8 Bible

Previous Page
Next Page

Using the Delegate Class to Control Scope with Listeners

If you start to use components with your own Flash movies and nest components within other Movie Clip symbols or your own custom components, you might come across problems with your listener object's scope. Scope refers to how paths within the listener method are evaluated. For example, when you have the following basic function, the scope for the variable named sName is local to the function, not the current timeline:

var sName:String = "Goliath";
function onClick():Void {
   var sName:String = "David";
   trace("sName: "+ sName);
}
onClick();

If you add that code to your Flash movie and test it, the Output panel shows the value for the local variable named sName, not the sName value declared outside of the function:

sName: David

Similarly, the this scope can vary depending on how a function is called. If you add the following code to a Flash movie and test it, the Output panel displays the same value for this, inside and outside of the function:

trace("outside of function, this = "+ this);
function onClick():Void {
   trace("inside of function, this = "+ this);
}
onClick();

For this code, the Output panel displays:

outside of function, this = _level0
inside of function, this = _level0

However, if you use the same function, onClick, as a listener of a component, the this scope will refer to the component instance, not the owner of the onClick function (_level0):

var cbt:mx.controls.Button;

trace("outside of function, this = "+ this);

function onClick(oEvent:Object):Void {
   trace("inside of function, this = "+ this);
}

cbt.addEventListener("click", onClick);

If you tested the movie with this code and clicked the cbt instance, the Output panel displays:

outside of function, this = _level0
inside of function, this = _level0.cbt

Generally, you'll find that you want to keep the scope of your listener objects under control, and not necessarily executing within the scope of the component emitting the event. This issue becomes more of a necessity to address with larger application architectures, but if you know the problem upfront, you'll save yourself many headaches in future Flash development. Luckily, the solution is pretty simple. If you use the Delegate class, you can control the scope of any handler very easily, regardless of whether the handler is a listener or a data handler for an XML object. The implementation of the Delegate class involves just two lines of code. In the following code sample, the Delegate class is imported and used to re-scope the onClick() handler to the Main Timeline (that is, _level0) for the Button listener:

import mx.utils.Delegate;

var cbt:mx.controls.Button;

trace("outside of function, this = "+ this);

function onClick(oEvent:Object):Void {
   trace("inside of function, this = "+ this);
}

cbt.addEventListener("click", Delegate.create(this, onClick));

Here, the Delegate.create() method is used to build a new listener object that controls the scope of the onClick() handler. The create() method takes two parameters: the scope reference you want to use, and the handler that will be called. For our code example, the scope reference is the current timeline (_level0) and the handler is onClick. If you tested the above code in a Flash movie that contained a Button instance named cbt on the Stage, the following text would be displayed in the Output panel when you click the instance:

outside of function, this = _level0
inside of function, this = _level0

Now, the scope is identical, inside and outside of the function. Let's practice using the Delegate class with the button_listener.fla file you built in the last section.

  1. Open the button_listener.fla file you created in the last section, or make a copy of the same file from the ch33 folder of this book's CD-ROM. Resave this file as button_listener_delegate.fla.

  2. Select frame 1 of the actions layer, and open the Actions panel (F9 or Option+F9). Replace the code with the following script:

    import mx.utils.Delegate;
    
    var cbt:mx.controls.Button;
    
    function onClick(oEvent:Object):Void {
       trace("onClick >")
       trace("\tthis = "+ this);
       for(var i in oEvent){
          trace("\t" + i + "= "+ oEvent[i]);
       }
    };
    
    cbt.addEventListener("click", Delegate.create(this, onClick));
    

    In this new code, you eliminate the need to create a new listener object — the Main Timeline (_level0) is the listener. The onClick() function is being delegated to handle the responsibility of the "click" event from the cbt instance.

  3. Save the document, and test it (Ctrl+Enter or z+Enter). When you click the cbt instance, the Output panel displays the following text. The this scope now refers back to the Main Timeline (_level0), not the component instance itself:

    onClick >
       this = _level0
       target = _level0.cbt
       type = click
    
Tip 

We often use the \t backslash pair in trace() statements to tab (or indent) messages within a function call.

On the CD-ROM 

You can find the completed file, button_listener_delegate.fla, in the ch33 folder of this book's CD-ROM.


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
Part VIII: Applying ActionScript
Part IX: Integrating Components and Data-Binding
Chapter 33: Using Components
What are Components?
Why Use Components?
How to Add a Component
Where to Find Components, Assets, and Parameters
Modifying Component Color Properties and Parameters
Removing Components from Your Movie
Components in Flash 8
Understanding the Listener Event Model for Components
Using the Delegate Class to Control Scope with Listeners
Using Components in Your Movie
Modifying Components
Using Embedded Fonts with Components
Replacing Component Skins
Custom Components
Summary
Chapter 34: Binding Data and Events to Components
Chapter 35: Building an Image Gallery Component
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