Macromedia Flash 8 Bible Free Open Book

Macromedia Flash 8 Bible

Previous Page
Next Page

Understanding the Listener Event Model for Components

While it's rather simple to drag and drop components on to the Stage of your Flash document and use the Property inspector to set up initial values for them, the result of any component behavior needs to be described by you, in your own ActionScript code. When the user interacts with a component, a specific event — tailored for the component — is sent out to the Flash movie. Just as you can yell when you're alone in a forest, these broadcasted events will just be ignored by the Flash movie unless you create something to listen to the events. Such objects are called listeners because their sole purpose is to intercept broadcasted messages from other objects that generate events — such as components.

What a Typical Listener Looks Like

Just about any object in a Flash movie can be a listener. You can add custom methods to MovieClip objects, for example, or you can create an instance of the Object class that exists only in ActionScript. To create a generic object, use the following syntax:

var oListener:Object = new Object();

or

var oListener:Object = {};

Note 

Since the components that ship with Flash 8 are built in ActionScript 2.0, you should get in the habit of using ActionScript 2.0 syntax for the code you use in movies using the components. If you use Flash 8 components, you must publish your Flash movie with ActionScript 2.0 selected in the Flash tab of the Publish Settings.

Once you have created an object that behaves as a listener, you can create methods for the listener geared toward the behavior of the component it observes. For example, if you wanted to do something when a Button component instance is clicked, you can add a click() method (or handler) to the listener object.

oListener.click = function(oEvent:Object):Void {
   trace("---listener received an event broadcast");
};

In this simple example, a trace() message is sent to the Output panel. You could add further actions to the click() method, performing more tasks necessary for your Flash movie.

Letting the Component Know Who its Listeners Are

Once a listener has a method to invoke, you need to assign the listener object (or its method) to the component that will be broadcasting events. For our previous example, you could assign the listener to a Button component instance named cbt by using the following code:

cbt.addEventListener("click", oListener);

or

cbt.addEventListener("click", oListener.click);

The addEventListener() method takes two parameters: the event being broadcast and the listener object (or method). Each component has its unique event names. The Button component, used in this example, has a click event, which is broadcast to all listeners when the user clicks the Button component instance.

Most components also broadcast additional information to the listeners, passed as parameters to the listener's method. All of the UI components pass an event information object to their listeners. The exact properties of this object vary from one component to the next, but some are consistent. In the following listener method, the oEvent parameter from the Button instance contains several properties, including oEvent.target, which is a reference to the Button instance.

oListener.click = function(oEvent:Object):Void {
   trace(oEvent.target + " was clicked.");
   trace("The broadcasted event was  "+ oEvent.type);
};

Note 

You can change the parameter name to any term you wish. In our example, the parameter is referred to as oEvent, but you can use a term such as eventObj. Just be sure to use the term consistently throughout your listener's method.

The type property, as you can see in the example code, can tell the listener what type of event was broadcast to it. In this way, you can create one method for a listener and have it handle different event types with specific code.

We'll show you how to create a simple example that demonstrates how a listener works:

  1. Create a new Flash document, and save it as button_listener.fla.

  2. Rename Layer 1 to cbt. This layer will hold an instance of the Button component.

  3. Open the Components panel (Ctrl+F7 or z+F7) and drag an instance of the Button component from the User Interface set to the Stage.

  4. Select the new instance, and in the Property inspector, name the instance cbt. Click the Parameters tab and assign a new label to the button, such as My Button.

  5. Create a new layer named actions, and place it at the top of the layer stack.

  6. Select frame 1 of the actions layer, and open the Actions panel (F9, or Option+F9 on Mac). Add the following code to the Script pane:

    var cbt:mx.controls.Button;
    
    var oListener:Object = new Object();
    oListener.click = function(oEvent:Object):Void {
       for(var i in oEvent){
          trace(i + "= "+ oEvent[i]);
       }
    };
    
    cbt.addEventListener("click", oListener);
    
    
  7. Save your document, and test it (Ctrl+Enter or z+Enter). When you click the cbt instance, you should see the following text in the Output panel:

    target = _level0.cbt
    type = click
    

    This output illustrates the two properties of the oEvent object broadcasted by the Button component. The event's name is "click" and the target property is a reference to the object broadcasting the event, cbt (_level0.cbt).

On the CD-ROM 

You can find the completed example file, button_listener.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