Hello everyone,

Trying to get a grasp on AS3.0. I know what addEventListerner is for and pretty much how to use it, but I the last three parameters in the statement have me scratching my head. There is a number (usually 0) and two Booleans--false and true respectively. Why are they there and do they ever change. If so, for what reasons(s)?

Thanks for your time in answering.

Recommended Answers

All 3 Replies

Event listeners are usually set up this way:

addEventListener([event you're looking for],[function to trigger once event happens]);

So an example would be:

myButton.addEventListener(MouseEvent.CLICK, handleClick);

It sounds like you may be talking about events themselves, though. Events are created using the following syntax:

new Event(type:String, bubbles:Boolean, cancelable:Boolean)

"Type" is the type of event and this can be constants (native to Flash such as '"MouseEvent.CLICK"') or custom types (created by you such as '"myCustomEventName"'). "Bubbles" refers to whether this event "bubbles" upward through the whole Flash application. Think of it this way: if an event "bubbles", it's as if the whatever triggered the event let out a bubble that floats upward through the Flash file and can be "seen" by the items above it. "Cancelable" refers to whether you can prevent the behavior associated with the event.

You can read all events and event listeners here: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fca.html

Rock on!

--eric

ok...I looked at the website you gave me and apparently there is a separate class called the IEventDispatcher class which contains more parameters. I must also add, however, that I am now more confused and don't know which to use and when when I am add an even listener. I originally thought the 3 parameter listener was sufficient with [event.event_type, function]. It seems there are additional conditions to consider adding, as I said, to my confusion.

Hey there!

Whoops - added confusion is not the goal :D

Maybe a different way to approach it would be to be more specific about what you're trying to achieve. Do you want to add event listeners to buttons? To functions? Can you explain a little more the problem you're tackling?

As a general overview, in AS3 you add event listeners whenever you want to track if some sort of event has happened and then have the program do something in response. For example, if you want to have the function "loadContent1()" get triggered whenever someone has clicked "button1", you would add the following code:

button1.addEventListener(MouseEvent.CLICK, loadContent1);
function loadContent1():void
{
    //Function info goes here
}

AS3 has events for pretty much everything: all the mouse behaviors (roll over, roll out, click, move, etc.), generic events (load, complete, etc.), and events specific to certain classes (data loading events, sound events, etc.). And if Flash's native events aren't enough, you can even add custom events if you like using the Event class.

The IEventDispatcher class is an INTERFACE, which you only will use in situations when you can't extend the EventDispatcher class (for example, if the class you're building is already extending another class). This all involves a deeper layer of complexity, so to avoid confusion, you should probably ignore the IEventDispatcher class for now and count on using the simple "addEventListener([event.event_type], [function])"

So to recap: Ignore IEventDispatcher for now, and focus on adding event listeners via "addEventListener([event.event_type], [function])", which you will use for most any scenario where you want to trigger a function if some sort of action/event takes place.

Best,
--eric

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.