PDA

View Full Version : Extending events, passing parameters to event handlers and controlling MCs


iblokh
11-28-2007, 07:00 PM
Ok, I assume this issue has been covered to death, but I'm still lost. In my application, I have numerous buttons which, when clicked, will activate numerous movie clips. To avoid having to create individual event ahndler functiosn for each button, I would like to be able to pass parameters to the event handler so it knows which movie clip to activate.

In other words, here's what I'd like to do:


for (i=0; i<spirals.length; i++) {
spirals[i].spiral.addEventListener(MouseEvent.CLICK, spiralUnroll);
spirals[i].text.addEventListener(MouseEvent.CLICK, spiralUnroll(spirals[i].spiral));
}


spirals[] is an array I built, filled with a custom class Spiral, which contains, amongst other attributes, an MC called spiral and an MC called text. When clicked, either one should activate the spiral MC's animation.

Now, I know the above syntax is incorrect and that I need to create my own event to do this - but all the examples I've seen show that I later have to dispatch the event myself. I, however, need the event to be dispatched on mouse click. Any ideas?

panel
11-28-2007, 07:10 PM
I don't see a point do dispatch own event here. Yo can just set

sprital.mouseChildern = fasle; //


and in the event handler function use currentTarget or target Property with will return reference to object dispatched event so you'll have access to all it's methods and properties


function onMouseDown(evt:mouseEvent):void
{
trace(evt.currentTarget)
}

pimpofpixels
11-28-2007, 07:14 PM
Maybe you should make the play animation function of the spiral part of the spiral class. Then you could pass that function to the listener. It seems strange I know to pass functions as variables but it works.

for (i=0; i<spirals.length; i++) {
spirals[i].spiral.addEventListener(MouseEvent.CLICK, spirals[i].spiralUnroll);
spirals[i].text.addEventListener(MouseEvent.CLICK, spirals[i].spiralUnroll);
}

iblokh
11-28-2007, 07:55 PM
and in the event handler function use currentTarget or target Property with will return reference to object dispatched event so you'll have access to all it's methods and properties


function onMouseDown(evt:mouseEvent):void
{
trace(evt.currentTarget)
}


Yeah, but the problem is while the target is the text mc, I need to activate the spiral mc that corresponds to it. So while I can access text, I don't see how I can get to the spiral mc from it, unless I'm missing something.

Pimpofpixels, that's a good idea - didn't occur to me. I'm getting some errors with my implementation of it now, trying to work it out.

panel
11-28-2007, 08:17 PM
evt.currentTarget.parent //reference to sprial


BTW
can't you just add one listener to sprial?

iblokh
11-28-2007, 08:38 PM
evt.currentTarget.parent //reference to sprial


BTW
can't you just add one listener to sprial?

Yeah, but both the spiral mc and text mc are in the main timeline (since I have multiple instances of each), so evt.currentTarget.parent gives me the timeline.

What do you mean by add one listener?

Oh and creating the unrollSpiral function in the custom class worked, just had to make the function public.

panel
11-28-2007, 09:11 PM
Class is like template so you can cretae class containing spiral and text + unrollSpiral . Then when you'll need new button you'll just create class instance and you'll add mouse event listener to this instance