- Modifié
[AS3/Starling] Be able to really listen an event
Hi guys,
So far all "events" are actually Functions that make impossible to add different "listeners/callbacks" for a same kind of events. I find this very annoying and not useful at all. Sometimes I need to listen the end of a specific animation in different places/classes in my code - Sometimes I want to listen for it only once but I cant remove the "listener/callback" because it will delete it for all animations of the skeleton. It's really not easy to manage it and it's clearly not the common way to achieve that in ActionScript.
It will be better to be able to register/unregister for and event like this :
mySkeleton.state.addEventListener(EventName,callback)
mySkeleton.state.removeEventListener(EventName,callback)
Or maybe by using Signals object in order to be able to dos this :
mySkeleton.state.onComplete.add(callback)
mySkeleton.state.onComplete.remove(callback)
Please consider change this because the current way is very limited compared to what ActionScript can proposed.
Thanks
I've made the listeners on AnimationState a Vector.<Function>.
Hi Nate,
That's great but I don't understand why you are using vectors instead of make the AnimationState Class inherited from EventDispatcher in order to be able to really add/remove listeners or you can also use Robert Penner Signals framework : https://github.com/robertpenner/as3-signals which is great and totally suitable for your needs in that case.
Thank you !!!
as3-signals looks nice, but seems like a lot of code to pull in for something so simple. Doing so would be fine if developing an app, but it can be annoying for a library to pull in things you don't want to use.
EventDispatcher looks not all that nice, plus seems to be overkill.
Vector is very simple and you can "really" add/remove listeners just fine:
event.splice(event.indexOf(func), 1);
That Vector's API sucks isn't my fault.
Possibly it would be ok to create a simple event listener class that has a Vector and add/remove methods to make the API a bit nicer.
Hi Nate,
I agree that inherited from EventDispatcher is not that much a good thing. About Signals I understand your point of view but I wanted to point that solution because it is very elegant and useful and really do the job in that case.
So ok, let's do it with Vectors but it's not that simple/elegant as you wrote because it mean thats you have to track the length of the Vector after you add a callback function in order to be able to remove it after. It's like not really having an add/remove functions but do it manually and track indexes by yourself.
It's the code in my last post to remove, not keeping track of an index.
var func:Function = function () {
// stuff
}
skeleton.state.onStart.push(func);
// then later:
skeleton.state.onStart.splice(skeleton.state.onStart.indexOf(func), 1);
Still not pretty. Will look at improving it.
Committed, now it looks like:
var func:Function = function () {
// stuff
}
skeleton.state.onStart.add(func);
// then later:
skeleton.state.onStart.remove(func);
Just hides the Vector ugly.
Like it !!!
I hope I didn't bother you to much on that topic, but it was (at least for me) very important.
Thank you very much Nate !
Not a bother at all, users should be vocal about pain points so things improve. 