PDA

View Full Version : Target



alemieux34
March 2nd, 2010, 01:32 PM
Quick question, what's the different between these:

e.target
e.content
e.currentTarget

???

I get the basic concept, you're targeting the object that you want to use from the listener in the function. But when do you use which?

BoppreH
March 2nd, 2010, 02:57 PM
Events don't have a "content" property. I guess you are confusing it with the loader.contentLoaderInfo property that is usually associated with events.

e.target returns the target that triggered the event listener. e.currentTarget returns the object that is being "listened" to.

This might look the same, but there's a special property of events called bubbling. Bubbling means that when one child dispatches an event, all the parents all the way down to the stage will dispatch the same event in sequence.

Some events "bubble", others not. MouseEvents, for example, do bubble because when you click a child, but has an event listener added to the parent, the listener is triggered. A text field's TextEvent.TEXT_INPUT does not bubble, meaning that you can't add a listener for "TextEvent.TEXT_INPUT" on the text field container and expect it to be triggered. TimerEvents can never bubble because they are not display objects and thus can never have parents.

So, bubbling is the process of an event traveling from the uppermost child all the way down to the stage.

e.target returns the uppermost child. e.currentTarget returns the object that is currently dispatching the event and thus the one that had the event listener actually added to.

You use e.target when you want to know precisely where the interacted (I can't think of any events that bubble by default aside from MouseEvents), because it'll return the smallest object. This is useful if you want to paint this object, for example.

e.currentTarget is used when you want to compare the object to something. When you add a listener to an object, you expect that the function triggered have a reference to that object in first place. That is the e.currentTarget. You use it when you want to know if the user clicked on the btnCancel or btnYes, because if you use e.target it might return that little shadow just above the button.

More on bubbling: http://www.rubenswieringa.com/blog/eventbubbles-eventcancelable-and-eventcurrenttarget

alemieux34
March 2nd, 2010, 03:34 PM
OK, I knew about bubbling, but didn't make the connection. That makes a lot more sense now.

Thanks.


Events don't have a "content" property. I guess you are confusing it with the loader.contentLoaderInfo property that is usually associated with events.

e.target returns the target that triggered the event listener. e.currentTarget returns the object that is being "listened" to.

This might look the same, but there's a special property of events called bubbling. Bubbling means that when one child dispatches an event, all the parents all the way down to the stage will dispatch the same event in sequence.

Some events "bubble", others not. MouseEvents, for example, do bubble because when you click a child, but has an event listener added to the parent, the listener is triggered. A text field's TextEvent.TEXT_INPUT does not bubble, meaning that you can't add a listener for "TextEvent.TEXT_INPUT" on the text field container and expect it to be triggered. TimerEvents can never bubble because they are not display objects and thus can never have parents.

So, bubbling is the process of an event traveling from the uppermost child all the way down to the stage.

e.target returns the uppermost child. e.currentTarget returns the object that is currently dispatching the event and thus the one that had the event listener actually added to.

You use e.target when you want to know precisely where the interacted (I can't think of any events that bubble by default aside from MouseEvents), because it'll return the smallest object. This is useful if you want to paint this object, for example.

e.currentTarget is used when you want to compare the object to something. When you add a listener to an object, you expect that the function triggered have a reference to that object in first place. That is the e.currentTarget. You use it when you want to know if the user clicked on the btnCancel or btnYes, because if you use e.target it might return that little shadow just above the button.

More on bubbling: http://www.rubenswieringa.com/blog/eventbubbles-eventcancelable-and-eventcurrenttarget