-
Yes, overloading is one thing that is missing in AS3... maybe AS4?
-
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);
}
-
Hi,
long time no write... actualy I wouldn't use this method anymore
But anyway, you could alwats use a weak listener (see addEventListener function)
OULALALALLALAOUAOUALALLALL...
I hate signature
-
Thanks a lot that listener with arguments is great.
-
To anyone that ended here looking for a simple solution to a problem like Andreq85's, it does exist:
PHP Code:
var someHandlerFunc:Function = someFunc(someArgs);
myBtn.addEventListener(MouseEvent.CLICK, someHandlerFunc);
//myBtn.removeEventListener(MouseEvent.CLICK, someHandlerFunc);
function someFunc(someArgs:Object):Function {
return function(e:MouseEvent):void {
trace(someArgs);
};
}
For multiple elements you can do:
PHP Code:
btn1.addEventListener(MouseEvent.CLICK, addPage(1));
btn2.addEventListener(MouseEvent.CLICK, addPage(2));
btn3.addEventListener(MouseEvent.CLICK, addPage(3));
function addPage(_nPage:int):Function {
return function(e:MouseEvent):void {
nPage += _nPage;
};
}
-
Originally Posted by metafurionx
To anyone that ended here looking for a simple solution to a problem like Andreq85's, it does exist:
PHP Code:
var someHandlerFunc:Function = someFunc(someArgs);
myBtn.addEventListener(MouseEvent.CLICK, someHandlerFunc);
//myBtn.removeEventListener(MouseEvent.CLICK, someHandlerFunc);
function someFunc(someArgs:Object):Function {
return function(e:MouseEvent):void {
trace(someArgs);
};
}
For multiple elements you can do:
PHP Code:
btn1.addEventListener(MouseEvent.CLICK, addPage(1));
btn2.addEventListener(MouseEvent.CLICK, addPage(2));
btn3.addEventListener(MouseEvent.CLICK, addPage(3));
function addPage(_nPage:int):Function {
return function(e:MouseEvent):void {
nPage += _nPage;
};
}
Nifty trick for a one-off, and for lazy prototyping, but I think it would be a bit risky for a production system as it makes clean up code rather tough eg: it's rather tough to find a function reference to removeEventListener!
All the same, thanks for some food for thought.
-
Originally Posted by AlsoGaiusCoffey
Nifty trick for a one-off, and for lazy prototyping, but I think it would be a bit risky for a production system as it makes clean up code rather tough eg: it's rather tough to find a function reference to removeEventListener!
All the same, thanks for some food for thought.
"Nifty trick"? "One-off"? "Lazy prototyping"? "A bit risky"? "Rather tough"? "Some food for thought"?
Wow, you desperately tried to repress the solution I just offered. That was extremely rude.
No, I didn't make this up. The approach is solid and widely used, let alone it being ancienter than this thread.
stackoverflow.com - To pass a parameter to event listener in AS3 the simple way… does it exist?
actionscript.org - adding parameters to function in addEventListener
kirupa.com - How to pass arguments from an event listener in AS3 - SIMPLE
life.neophi.com - Removing Anonymous Event Listeners
Originally Posted by AlsoGaiusCoffey
it's rather tough to find a function reference to removeEventListener
PHP Code:
btn1.addEventListener(MouseEvent.CLICK, addPage(1));
btn2.addEventListener(MouseEvent.CLICK, addPage(2));
btn3.addEventListener(MouseEvent.CLICK, addPage(3));
function addPage(_nPage:int):Function {
return function(e:MouseEvent):void {
nPage += _nPage;
e.currentTarget.removeEventListener(e.type, arguments.callee); // Run once and remove itself
};
}
PHP Code:
function addPage(_nPage:int):Function {
return function(e:MouseEvent):void {
nPage += _nPage;
};
}
var add1:Function = addPage(1);
var add2:Function = addPage(2);
var add3:Function = addPage(3);
btn1.addEventListener(MouseEvent.CLICK, add1);
btn2.addEventListener(MouseEvent.CLICK, add2);
btn3.addEventListener(MouseEvent.CLICK, add3);
// Remove when you need to
btn1.removeEventListener(MouseEvent.CLICK, add1);
btn2.removeEventListener(MouseEvent.CLICK, add2);
btn3.removeEventListener(MouseEvent.CLICK, add3);
Is this rather tough for you?
-
Certainly not intended the way you took it.
For the record, though, "possible" is not the equivalent of "easy" just as "precedent" is not a synonym for "good practice".
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
|