Flash Kid Logo  
Home - Tutorials - Applications - Games - Books - Directory - Community - Site Map - Contact Us  
 
 


 

Basics of AS3 Event Listener in Flash
Author: Salman Awan
Difficulty Level: Intermediate
Flash Version: ActionScript 3
Prerequisites: Action Script Basics, Display Programming Basics

Introduction:

Purpose of this tutorial is to provide basics of Event Listening concepts in ActionScript 3.0. This tutorial does not contain any actual code examples, however it will enable you to understand the core concepts and overall flash events workflow in Action Script 3.0.
 
 

Let us start with some basic definitions:

Definitions

Event:
You can think of events as occurrences of any kind in your SWF file that are of interest to you as a programmer.

For example, most SWF files support user interaction of some sort - whether it's something as simple as responding to a mouse click or something more complex, such as accepting and processing data entered into a form. Any such user interaction with your SWF file is considered an event.

Events can also occur without any direct user interaction, such as when data has finished loading from a server (e.g. loading external xml file) or when an attached camera has become active.

Event Handling System:
An event-handling system allows programmers to respond to user input and system events in a convenient way.

An Event Handling System can be understood by dividing in these three components:

  • Event Object: Instance of an Event class, containing all the information related to that particular event in swf.
  • Event Flow: The event flow means, how an event object flows through Display List hierarchy.
  • Event Listeners: Functions hooked to the Event Flow that will intercept an Event Object and perform their function accordingly.


Examples of Uses of Events

Let’s discuss some real life examples where you might need to implement Events Handling in ActionScript 3.0.

Example 1: You have a button in your SWF file and u want a message window to pop-up inside on release actionscript.

Example 2: You have loaded an external xml file, and want to process and customize your swf, when xml is completely loaded.


AS3 Events Workflow

Let us take first example above and have a look at general Events workflow.
Say we have created a button on Stage. This button is going to be our Target, an Object on which an event occurs.

Now we need to define a function that will be executed when user clicks on that target button, termed as Event Listener, because it will actually listen and handle that event.

Generally a listener event is defined like this. With appropriate function name, event object name and event type.

function eventResponse(eventObject:EventType):void
{
 // Actions performed in response to the event go here.
 }


But we haven’t established a link between this function and the button. How would SWF know which function to execute when button is clicked?

For letting the swf know, we would need to explicitly register this function against the target button, so that it will be notified whenever button is clicked.

Here’s a general template used for registering an as3 event listener against a target.

eventTarget.addEventListener(EventType.EVENT_NAME, eventResponse);

eventTarget in above line will be the Button instance name in our case. EventType.EVENT_NAME is actually a string, in our case it will represent a mouse click. And eventResponse is the function name we created above, that will listen to the clicks and executed once button is clicked.

This registration and listener process is similar to our mechanism of Email Subscription. You subscribe to an email list and whenever a new message comes you are notified. Similarly, whenever an event occurs, it’s  Listeners are notified and executed.

So now when Flash Player detects a mouse click, it creates an event object (an instance of the MouseEvent class) to represent that particular mouse click event.
After creating a event object, Flash Player dispatches it, which means that the flash event object is passed to the object that is the target of the event, Button instance in our case.

If our button is inside another display object, say inside another sprite, then dispatched even will be passed to Sprite first, and then it will move down in hierarchy of display objects till it reaches it’s actual target i.e. Button. This traversal of event object is called Event Flow.

As soon as Button will receive the Event Object, it will determine it’s type which is MouseClick in our case, it will notify all the actionlisteners registered with Button instance, that button has been clicked.

As a result, our eventResponse function will be executed above, and perform any operations coded inside it, a message window to pop-up in our case.

 
 
 
 
 
 
 
Related Links
Discussion Forums for Tutorials
 
 
 
Feedbacks :
 
 
 
Send Feedback
Your Name:
Email:
Comments:
 
Code:
 
 
 
 
 
 
© 2010 flashkid.org! All Rights Reserved.
 
   
 
a project of: salmanawan, other projects: xonsolutions.com