PDA

View Full Version : Extending mouseEvent class


baddriverdave
02-11-2008, 06:37 PM
Hey guys,

I'm trying to see if I can add a parameter to pass a Number Variable through AddEventListener. I'm trying to create a custom class. Here's what I have thus far.


package
{
import flash.events.MouseEvent;

public class MapMouse extends MouseEvent
{
public var _mapNum:Number;

public function MapMouse(MapNumber:_mapNum):void
{
this.addEventListener(MouseEvent.CLICK, _mapNum, mapChange);
}
}
}


This would be called using an EventListener, with an additional parameter, looking something like this.


this.mapMC.TAMUmap.mapGraphic.academic_btn.addEven tListener(MouseEvent.CLICK, mapChange, _mapNum);



But I'm getting errors thrown at me when I try to run this:

1046: Type was not found or was not a compile-time constant: _mapNum.

and

5000: The class 'mapMouse' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.

Any suggestions would be greatly appreciated. THANKS!!!

panel
02-11-2008, 06:45 PM
You cannot add param to addEventListenerMethod this way. What exactly you wanna to achieve?

baddriverdave
02-11-2008, 06:59 PM
I'm trying to send data from whatever the click is to the "mapChange" function, specifically trying to send an extra parameter, data-typed as a number. Do you know how I could achieve this?

Thanks!

panel
02-11-2008, 07:12 PM
Still it's hard to say what is you goal. Why you waht to pass _mapNum variable since it can be easly accessed in handler function (mapChange) :eek:

Mabye try yo explain step by step what should happen in application.

baddriverdave
02-11-2008, 07:32 PM
Thanks Panel..

I guess my question is how do I access those variables from my handler function.

In the application, a person would click on one of multiple links, and dependent on what they would click it would send a variable through to the handler function, which is where I'm hung up.

Thanks for your help!

panel
02-11-2008, 08:36 PM
button.addEventListener(MouseEvent.CLICK, onMouseClick);
button2.addEventListener(MouseEvent.CLICK, onMouseClick);
button3.addEventListener(MouseEvent.CLICK, onMouseClick);


function onMouseClick(evt:MouseEvent):void
{
evt.currenttarget //this is reference to object witch has been clicked

evt.currenttarget.mapNum //I would put map num variable as property and access it like this in handler function
}

you can also writesome expresion to get map number just knowing with button was pressed

Digression
02-22-2008, 07:48 PM
Ok, I have a similar question to this process. I wish to attach event listeners to some movieclips, to have a caption pop up. The problem I am having, is I need to run a loop to attach the listeners to my movieclips that I have referenced through an array (mcArray4).

I have 25 movieclips that each need a listener to show a caption box when you roll over it.

Here's some code to show more of what I mean.

var mcArray4:Array = new Array;
for (var i=0;i<24;i++){
mcArray4.push(container.getChildByName("mc_"+i));
mcArray4[i].addEventListener(MouseEvent.MOUSE_OVER,captionOn) ;

function captionOn(event:MouseEvent):void {
addChild(combo);
combo.caption.text = captionArray[i]; //captionArray contains my captions from an XML file
}
}


What happens here, is I end up only pulling the VERY LAST caption and displaying it for all the clips. I.E. they all end up saying "Caption24". I have my captionOn function INSIDE my for loop to try to read "i" properly, but that failed.

What I am looking for (and I think partially what baddriverdave is looking for) is a way to send my "i" variable with my listener, so that I can tell which part of my array to assign to which movieclip.

Example:
mcArray4[16] matches up with captionArray[16]. However, what I end up with currently is captionArray[24], instead.

I'd like to do something like this:

//loop
mcArray4[i].addEventListener(MouseEvent.MOUSE_OVER,captionOn( sendAVariable));
//end loop

//inside function
combo.caption.text = captionArray[sendAVariable];
//end function


Does this make sense, and is it possible?