-
e.target.name not working for MOUSE_UP listener!
Code:
var buttons:Array = new Array(
_button1,
_button2,
_button3
);
for(var i=0; i<3; i++) {
MovieClip(buttons[i]).buttonMode = true;
MovieClip(buttons[i]).addEventListener(MouseEvent.ROLL_OVER, buttonOver);
MovieClip(buttons[i]).addEventListener(MouseEvent.ROLL_OUT, buttonOut);
MovieClip(buttons[i]).addEventListener(MouseEvent.MOUSE_UP, buttonClicked);
}
function buttonOver(e:MouseEvent):void {
trace(e.target.name);
}
function buttonOut(e:MouseEvent):void {
trace(e.target.name);
}
function buttonClicked(e:MouseEvent):void {
trace(e.target.name);
}
I have 3 buttons on my stage (this is a small part of a larger project), named _button1, _button2, _button3 declared in an array so I can dynamically assign listeners and use a substring of the instance name to determine which button was clicked for other functions.
My problem is that trace(e.target.name); returns the value _button1 on ROLL_OVER and ROLL_OUT as expected. However when clicking the button it returns "instance9" instead of the actual instance name I gave it.
Why is this happening and how do I make it return the correct name? Why the hell would my buttonClicked function return a different result from the buttonOver function where the target is the same button for each function?
Thanks to anyone who can help me.
-
Upon further inspection the instance name being returned from "MOUSE_UP" are movieclips WITHIN the button MC (which is a movieclip with buttonmode=true). It returns on whatever piece of the button I have clicked on within the movieclip.
How do I get it to return the name of the containing MC in this case and not the children of the button MC?
-
Nevermind...
MovieClip(buttons[i]).mouseChildren = false;
Adding that line before adding the event listeners fixed the issue. Hopefully the post here will help someone else out with the same issue.
-
Registered User
hi,
Also try
Actionscript Code:
function buttonClicked(e:MouseEvent):void { trace(e.currentTarget.name); }
There's a difference between target and currentTarget.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|