ricardo.poblete

Adobe Flex, ActionScript 3, etc…

Archive for April, 2008

Understand Events in Flex with examples, part 1

Posted by ripoblet on April 18, 2008

Today I begin a comment in examples that show you how manage events.

I separate this comment in 6 parts, this is the first part I talk about three basic concepts event, listener and dispatch in simple words.

Event: Indicate that something happend in somewhere. Is an object that have information about who triggered.

Listener: This is a method that can “listen” or “hear” the event and do something when it happens (handler function)

Dispatch: This is a method that can trigger an event.
Ok, let’s begin

 

Example_01

 <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
            <mx:Panel x="17.5" y="20"
		width="209" height="142" layout="absolute"
                  click="mx.controls.Alert.show('click event on the panel')"
		title="Panel">
            <mx:Button label="ClickMe"
		x="60.5" y="38"
                  click="mx.controls.Alert.show('click event on the button')"/>
            </mx:Panel>
</mx:Application>

 Save and run the example

Listeners: Panel and button component are listen the click event.   Both cases “Alert” is the handler unction

Dispatch: Flash player will trigger the event when user click on the button or somewhere in the panel.

Event: MouseEvent.CLICK

 

You will see an important effect, bubble.

You must consider that are two components listening the same event and how the button is into the panel flash player propagate the event throgh the all components thar are registered to listen the event.

You are experimented the three phases in a flow event: Capturing, Target and Bubbling.

In the capturing phase, the plash player search every object who want to listen the event triggered recently, then, when find the object that triggered the event (capturing phase), it set some properties about the target and finally propagate (bubbling phase) the event to every object who want do something when “hear” or “listen” the event.

So, take care when two object are listening the same event, because they will do something.

 

In the next sections we will see how can do the same thing, but with actionscript.

 

Regards.

ripoblet

Posted in Flex | Tagged: | Leave a Comment »