PDA

View Full Version : Passing objects to eventhandlers



MarMel
December 4th, 2008, 04:22 PM
Hy Guys!

Let me first introduce myself: I am Marcel, I live in the Netherlands.. I'm fairly new to Kirupa, that is, new to posting my problems/findings. I'm using flash (CS 3) now for allmost a year, and the threads in kirupa helped me through sometimes!

Now, my problem. I'm making my own Portfolio, and i have a problem with eventhandlers. I have a couple different objects, lets call em A, B, etc. I want to do the same things to these objects, when i hover over, or click on them. I want to do this with, obviously, event handlers. The tricky part is, i want to make a bunch of eventhandlers for every object, but all handlers have to call the same function. To do this, i have to put the object as an variable in the event handler. Only, this is not possible in AS3! How can i do this?

Since this is my first thread here, i hope it is clear for you guys! If it isn't, just ask!

Greetings!
Marcel

senocular
December 4th, 2008, 04:28 PM
You can access the Object (if by object I assume you mean A, B, etc.) through Event.currentTarget

Sage_of_Fire
December 4th, 2008, 04:59 PM
Enlighten me, oh mighty senocular; why not use Event.target?

MarMel
December 4th, 2008, 05:16 PM
Wow, how easy can a sollution be! It works! Thanks!

senocular
December 4th, 2008, 05:59 PM
Typically, when you reuse a function for multiple events (or the same event) across many objects, what you want in that function - the event handler - is a reference to the unique object whence the event orginated. For most cases, this is the same object on which you used addEventListener. So if you have buttons A, B, and C, and you each give them a CLICK listener using the same event handler, in that handler you want to know what was clicked, A, B, or C. Event.currentTarget gives you this value (currentTarget being the object used with addEventListener).

Event.target will give you the lowest-most interactive display object capable of generating the event. In the case of buttons and other complex objects, this could potentially be some graphics sprite or movie clip that makes up the visuals of the button, not the button itself.

Note that this only applies to bubbling events such as mouse events. For those events that don't bubble, target and currentTarget are the same.