To talk about the features of NResponder I want to give a little comparison.
In the following table you can see the native Envent Engine (or API) compared to the features of NResponder.
|Use without instance|
|Use with instance|
|Specify number of dispatch arguments|
|Dynamic dispatch arguments|
|Change dispatch arguments in Run-Time|
|Specify a delay time|
|Set the number of replies|
|Can associate with a target|
|Can pause without remove|
|Can stop propagation|
|Change attributes in Run-Time|
|Garbage Collect||only weak references|
I will focus on the most important and more usable features:
Static and instantiated mode
With NResponder you can choose the best to way to work. Use it as a static class, without create any constant or variable, or use it as instance, a more object oriented approach.
A important thing to say is that the instantiated mode offers to you more control over the event, like pause it without remove.
Custom number of arguments and changes
This is really one of my favorite features. As you know, in the native Event Engine of Flash the dispatcher MUST and NEED to inform ONE, and just one, Event instance. And the call back functions need to expect that kind of event.
So, for example, what you have is:
private function callBackFunction(event:MyCustomEvent):void
This is boring!!!
If you want to pass some kind of data to your
callBackFunction()you need to put new variables to your
Just forget it!!!
With NResponder you can pass how many parameters you want and can have a call back function like this:
private function callBackFunction(aNumber:Number, aString:String, customObject:ACustomObject):void
This give to you so much more flexibility to work!
Because you can make your call back functions more readable and more reusable.
Number of replies
This is one of my favorite too.
With this feature you can simple set the number of times that a specific event (or Action, in NResponder language) will happen!
How many you want, just 5, or until 21, anything!
One very common and useful usage of this is for view classes.
As you know, when you start a view class and it is not on the stage yet, its property of "stage" is null. So a technique widely accepted among developers is the use of:
public function Main()
public function init()
You need to add and remove, make checks to hasEventListeners, to use this routine just ONCE!
With NResponder all that you need is set the number of replies, in this case, to 1 and stay focus on your code!
Because NResponder will be responsible to later clean up the mess, delete what it need to delete, frees the memory and go on.
This can be very helpful for some situations.
The delay time specify the time, in seconds, that will be the minimum to wait between two consecutive dispatches.
For example, you have a button in your game that the user need to press continuously. But you want a little interval of time between two clicks, like 0.5 secs. With NResponder all that you need is set one property and everything will be controlled by the NResponder engine.
With the native Event Engine if you set a lot of listeners and after need to remove that listeners, you need to remove one by one.
With NResponder you can make combined removes, like remove all listeners of a target, remove all listeners of a specific call back function, remove all listeners of a specif type (Action), like
Mouse.CLICK, or even make combination like remove all listeners of a specific target with a specific call back function.
Don't worry if this don't make sense to you now, when I show the examples, everything will becomes clearer.
OK, these are the most important features of NResponder.
I will not talk about features that have a correlated in native Event Engine, like order or stop propagations. Because I suppose that you already know from EventDispatcher.
One more thing...
Something that you may think now is: "OK, you talk about mouse events, keyboard, touches... and said that I can say Goog Bye to native Event Engine... but are not mouse, keyboard and touches native events?".
You are right!
With NResponder you don't need any instance of EventDispatcher in your application, you don't need to use the processing core of native Event Engine.
But if you want, NResponder give you a way to use the native system under the NResponder engine system.
Obviously to work with mouse envets, keyboard, touches, etc, NResponder need to use a little portion of the native Event Engine, because is the only way to catch this kind of event in Flash.
But again, NResponder can do this so much better than using the native EventDispatcher process.