-
AS3 pass arguments in addEventListener
Hi, i gather it's not possible to send arguments from Mouse Events without writing a custom EventManager. What would be a correct way to identify either the object sending the event or to pass arguments to a function specified in the Mouse Event?
I discovered this but I'm not sure if it's appropriate since i'm not familiar yet with EventManager/Dispatch stuff, I don't know what I'm looking for:
http://www.kirupa.com/forum/showthread.php?t=291250
Hypothetical:
I have a function which receives a MovieClip object then adds a listener to it - I want to then pass the same object onto the next function if the object receives a mouse click.
Code:
//ADD EVENT LISTENERS
public static function addFirstClickListener(myButton:MovieClip) {
myButton.addEventListener(MouseEvent.CLICK, doSomething(myButton));
}
PS: trace for "myButton" turns out correctly
button passed is [object news]
Last edited by bosie; 05-31-2008 at 03:14 PM.
bosie
-
Senior Member
- The right of the People to create Flash movies shall not be infringed. -
-
hello everyone, i'm a young starter(19) and i found this with a bit of a mix of javascript , however i don't know how to access evt.target in the function then... and don't know how to remove the listener also afterwards
PHP Code:
addEventListener(MouseEvent.CLICK,function() {klik("functiontest")});
public function klik(s:String):void {
trace(s);
}
-
5+5=55
Originally Posted by jensmct
hello everyone, i'm a young starter(19) and i found this with a bit of a mix of javascript , however i don't know how to access evt.target in the function then... and don't know how to remove the listener also afterwards
PHP Code:
addEventListener(MouseEvent.CLICK,function() {klik("functiontest")});
public function klik(s:String):void {
trace(s);
}
Try something like:
PHP Code:
addEventListener(MouseEvent.CLICK,mouseClick);
public function mouseClick(event:MouseEvent){
klik("functiontest",event);
}
public function klik(s:String,event:Event):void {
trace(s);
trace(event.target);
}
To remove the listener, use:
PHP Code:
removeEventListener(MouseEvent.CLICK,mouseClick);
(Sorry about not answering the original post, but I think cancerinform's link should help with that)
-
55 that's no good...you're not actually sending a variable, just calling a second function with the same hard-coded var in it...
Flax dropped some cool closure code on this problem a while back.
-
5+5=55
Originally Posted by neznein9
55 that's no good...you're not actually sending a variable, just calling a second function with the same hard-coded var in it...
Flax dropped some cool closure code on this problem a while back.
I know, but for what jensmct asked, it should work fine.
-
thx people but i already knew that, just searching for a new way to adress your problem
-
Whoops missed the first post :P
All these solutions are overkill for that specific problem - there are two attributes of Event that will give you what you need:
PHP Code:
myButton.addEventListener(MouseEvent.CLICK, doSomething);
private function doSomething(e:MouseEvent):void{
trace(e.currentTarget); // myButton
trace(e.target); // myButton or a child of it
}
Event.currentTarget will give you the object that you attached the listener to - so for that code it would always be a reference to myButton.
Event.target will give you the object that triggered the event - so if myButton had a movieclip and a textfield inside it, you might get either object as the target depending on which _part_ of the button you clicked. It's a subtle distinction but it can save you a lot of headaches.
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
|