<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>db-in&#039;s blog &#187; ActionScript3.0</title>
	<atom:link href="http://db-in.com/blog/category/actionscript3/feed/" rel="self" type="application/rss+xml" />
	<link>http://db-in.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 05 Mar 2012 15:15:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>NResponder tutorial</title>
		<link>http://db-in.com/blog/2010/11/nresponder-tutorial/</link>
		<comments>http://db-in.com/blog/2010/11/nresponder-tutorial/#comments</comments>
		<pubDate>Tue, 16 Nov 2010 00:21:08 +0000</pubDate>
		<dc:creator>Diney Bomfim</dc:creator>
				<category><![CDATA[ActionScript3.0]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Nineveh]]></category>

		<guid isPermaLink="false">http://db-in.com/blog/?p=550</guid>
		<description><![CDATA[Introduction Hello everyone! Welcome to this article! Today I want to show you something called Nineveh Responder engine, or just NResponder. And to do so, I will cover just four points, nothing more: Nineveh What?: What is this? What is it for? NResponder&#8217;s features: What news does it bring? NResponder usage: How to use it?&#8230;]]></description>
			<content:encoded><![CDATA[<h2><strong>Introduction</strong></h2>
<p>Hello everyone!</p>
<p>Welcome to this article!</p>
<p>Today I want to show you something called Nineveh Responder engine, or just NResponder.<br />
And to do so, I will cover just four points, nothing more:</p>
<ul>
<li><strong>Nineveh What?</strong>: What is this? What is it for?</li>
<li><strong>NResponder&#8217;s features</strong>: What news does it bring?</li>
<li><strong>NResponder usage</strong>: How to use it?</li>
<li><strong>REAL WORLD example</strong>: NResponder in action!</li>
</ul>
<p>OK, so let&#8217;s start!</p>
<p>Here are the source files of NResponder and the project files used in this tutorial:</p>
<p><a href="http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip"><img src="http://db-in.com/imgs/source_button.jpg" border=0/></a><a href="http://db-in.com/frameworks/nineveh/tuto_1.zip"><img src="http://db-in.com/imgs/download_button.jpg" border=0/></a><br />
<span id="more-550"></span></p>
<h2><strong>Nineveh What?</strong></h2>
<p>Do you remember of the city Nineveh? OK, nothing like that.</p>
<p>Nineveh is a new Framework to AS3 under construction. The point here is not to talk about this Framework, but just is important to say that NResponder is one of the engines and APIs of the Nineveh Framework. Something new about this framework is that it has engines and APIs independents, like NResponder, and these engines and APIs can work fine outside the framework.</p>
<p>OK, but what the kind of engine is NResponder?</p>
<p>An Event Engine!</p>
<p>For years I&#8217;ve used the Event Engine of default Flash framework called EventDispatcher. Oh God!</p>
<p>How boring it was! You know:</p>
<ul>
<li> Create some kind of instance or inherit from EventDispatcher class.</li>
<li> Add listeners into it.</li>
<li>Create a VERY LIMITED AND STRICT event-class inherited from Event class (in majority of cases). And you need to use just event-class AS PARAMETER when dispatching.</li>
<li>Create a new instance of that custom event-class.</li>
<li>Dispatch your custom Event using the above instance of EventDispatcher (well, I mean, if you have some instance at the time of dispatch).</li>
<li>Wait for Capture phase, Target Phase, Bubbling Phase.</li>
</ul>
<p>Ooooh&#8230;  just STOP!!!</p>
<p>If you are boring with all of that, like me, NResponder is for you!<br />
If you are completely new to Events in Flash, NResponder is for you as well!<br />
And if you are new just delete the points that I just spoke above.</p>
<p>NResponder is much much faster than native Event Engine of Flash.<br />
You don&#8217;t need to create any instance, if you don&#8217;t want to.<br />
You can manage and control events of your entire application.</p>
<p>Imagine this scene:<br />
You start you application, place 3 buttons on the stage, and load an external SWF to play a video. When you press one button, the video playing in the other SWF needs to be stopped, because another SWF will be loaded above it.</p>
<p>Well&#8230;  with EventDispatcher you should have a big path to follow until finally catch it up.<br />
But with NResponder, all that you need, is just 2 lines of code!<br />
One in the Button class and another one in the video player! Just as simple as that!<br />
Well, this is just the natural behavior of NResponder, I haven&#8217;t even talked about features yet.</p>
<p>You can use NResponder for everything related to events or fire actions, mouse and keyboard and touch interactions, etc.<br />
As stated in the title of this article, you can completely say &#8220;Good Bye&#8221; to EventDispatcher.</p>
<p>OK, now you know what NResponder is.<br />
If you want, you can see the project page on Google: <a href="http://code.google.com/p/nineveh-responder/">http://code.google.com/p/nineveh-responder/</a></p>
<p>Check the blog page here: <a href="http://www.actionscript.org/resources/admin/de/editor.php?name=wysiwyg2&amp;refresh=33963203">http://db-in.com/blog/nineveh-framework/nresponder/</a></p>
<p>You can download the NResponder here: <a href="http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip">http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip</a></p>
<p>Obviously you can check the project page on Google to see the Documentation, check the SVN, learn with the Wiki and so forth, but here I will teach you step by step guide in a tutorial to use the great power of NResponder.</p>
<p>Now let&#8217;s dig further.</p>
<p><br/></p>
<h2><strong>NResponder&#8217;s features</strong></h2>
<p>To talk about the features of NResponder I want to give you a little comparison.</p>
<p>In the following table you can see the native Envent Engine (or API) compared to the features of NResponder.</p>
<table class="innertable">
<tbody>
<tr>
<th>Feature</th>
<th align="center">Event API<br />
(Adobe® Flash®)</th>
<th align="center">Signals<br />
(Robert Penner)</th>
<th align="center">NResponder<br />
(Diney Bomfim)</th>
</tr>
<tr>
<td><strong>Used without instance</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Used with instance</strong></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Specify number of dispatch arguments</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Dynamic dispatch arguments</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Change dispatch arguments in Run-Time</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Remove all</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center">limited to instance</td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Specify a delay time</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Set the number of replies</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center">Just 1</td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Ordenate</strong></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Can associate with a target</strong></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center">only NativeSignal</td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Can pause without remove</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Can stop propagation</strong></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Change attributes in Run-Time</strong></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
<tr>
<td><strong>Garbage Collect</strong></td>
<td align="center">only weak references</td>
<td align="center"><img src="http://db-in.com/imgs/no.gif" alt="No" /></td>
<td align="center"><img src="http://db-in.com/imgs/yes.gif" alt="Yes" /></td>
</tr>
</tbody>
</table>
<p>I will focus on the most important and more usable features:</p>
<p><br/></p>
<h4><strong>Static and instantiated mode</strong></h4>
<p>With NResponder you can choose the best way to work. Use it as a static class, without creating any constant or variable, or use it as an instance, a more object oriented approach.</p>
<p>An important thing to say is that the instantiated mode offers you more control over the event, like pausing it without remove it.</p>
<p><br/></p>
<h4><strong>Custom number of arguments and changes</strong></h4>
<p>This is really one of my favorite features. As you know, in the native Flash&#8217;s Event Engine 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.</p>
<p>So, for example, what you have is:</p>
<pre class="brush:as3">
dispatcherInstance.addEventListener("aCustomEvent", callBackFunction);
dispatcherInstance.dispatchEvent(new MyCustomEvent("aCustomEvent"));
...
private function callBackFunction(event:MyCustomEvent):void
{
...
}
</pre>
<p>This is boring!!!</p>
<p>If you want to pass some kind of data to your callBackFunction() you need to put new variables to your MyCustomEvent class!</p>
<p>Just forget it!!!</p>
<p>With NResponder you can pass how many parameters you want and can have a call back function like this:</p>
<pre class="brush:as3">private function callBackFunction(aNumber:Number,
                                  aString:String,
                                  customObject:ACustomObject):void
{
...
}</pre>
<p>This give you so much more flexibility to work!</p>
<p>Because you can make your call back functions more readable and more reusable.</p>
<p><br/></p>
<h4><strong>Number of replies</strong></h4>
<p>This is one of my favorite too.</p>
<p>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!<br />
One very common and useful usage of this is for view classes.<br />
As you know, when you start a view class and it is not on the stage yet, its property of &#8220;stage&#8221; is null. So widely accepted technique among developers is the use of the following:</p>
<pre class="brush:as3">
public function Main()
{
    if (this.stage)
    {
        init(null);
    }
    else
    {
        this.addEventListener(Event.ADDED_TO_STAGE, init);
    }
}

public function init()
{
    if (this.hasEventListener(Event.ADDED_TO_STAGE))
    {
        this.removeEventListener(Event.ADDED_TO_STAGE, init);
    }
}
</pre>
<p>You need to add and remove, make checks to hasEventListeners, to use this routine just ONCE!<br />
It&#8217;s sucks!!!<br />
With NResponder all that you need is set the number of replies, in this case 1, and stay focused on your code! Because NResponder will be responsible to clean up the mess later, delete what needs to, frees the memory and go on.</p>
<p><br/></p>
<h4><strong>Delay time</strong></h4>
<p>This can be very helpful in some situations.<br />
The delay time specify the time, in seconds, that will be the minimum to wait between two consecutive dispatches.<br />
For example, you have a button in your game that the user needs to press continuously. But you want a little interval of time between two clicks, like 0.5 secs. With NResponder all that you need to do is to set one property and everything will be controlled by the NResponder engine.</p>
<p><br/></p>
<h4><strong>Combined removes</strong></h4>
<p>With the native Event Engine if you set a lot of listeners and later you need to remove them, you&#8217;ll have to do so one by one.</p>
<p>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.<br />
Don&#8217;t worry if this don&#8217;t make sense to you now, when I show you the examples, everything will become clearer.</p>
<p>OK, these are the most important features of NResponder.</p>
<p>I&#8217;ll not mention the features that have correlated in native Event Engine, like order or stop propagations, because I suppose that you already know it from EventDispatcher.</p>
<p><br/></p>
<h4><strong>One more thing&#8230;</strong></h4>
<p>Something that you may be thinking now is: &#8220;OK, you&#8217;ve talked about mouse events, keyboard, touches&#8230; and said that I can say Goog Bye to native Event Engine&#8230; but are not mouse, keyboard and touches native events?&#8221;.<br />
Yeah!<br />
You are right!<br />
With NResponder you don&#8217;t need any instance of EventDispatcher in your application, you don&#8217;t need to use the processing core of native Event Engine.<br />
But if you want to, NResponder gives you a way to use the native system under the NResponder engine system.<br />
Obviously to work with mouse events, keyboard, touches, etc, NResponder need to use a little portion of the native Event Engine, because it&#8217;s the only way to catch this kind of event in Flash.<br />
But again, NResponder can do this so much better than using the native EventDispatcher process.</p>
<p><br/></p>
<h2><strong>NResponder usage</strong></h2>
<p>Hands to work!</p>
<p>For now we&#8217;ll just create a project that prints (trace) some responses on the screen. So you&#8217;ll need an IDE to compile and debug the code, like Flash Professional, Flash Builder, Flash Develop, FDT, etc.</p>
<p>OK, now let&#8217;s code!</p>
<p><br/></p>
<h4><strong>Static or instantiated mode</strong></h4>
<p>The first important step is to import the NResponder to your classes. Some IDEs do this automatically for you (Flash Builder, FDT, Flash Develop,&#8230;) when you are typing. NResponder package is inside the &#8220;appkit&#8221; (a SubFramework of Nineveh Framework) and inside the package &#8220;responders&#8221;. This path is just a convention of Nineveh Framework. Of course, you will need to import this package in all classes that you want to use NResponder.</p>
<pre class="brush:as3">
package
{
    import flash.display.MovieClip;
   
    import appkit.responders.NResponder;

    public class Main
    {
        public function Main():void
        {
        }
    }
}
</pre>
<p>Now, about the static and instantiated mode. Every time you add a new NResponder (register an event, add a call back function, register a listener, whatever, is the same thing for NResponder), the NResponder will create a little instance of Responder, this instance will go to the core of NResponder and will be the responsible to execute the commands in the engine&#8217;s core. This is a behavior, NResponder will always do this. The cool thing here is that you can get a copy of this instance when you add a new. And you have some flexible ways to do so:<br />
IMPORTANT: The following code is not real, is just to illustrate the both modes. In the next section (Creating a NResponder) you&#8217;ll see a real code.</p>
<pre class="brush:as3; highlight: [12, 15, 16, 18]">
package
{
    import flash.display.MovieClip;
   
    import appkit.responders.NResponder;

    public class Main
    {
        public function Main():void
        {
            // This is the static mode
            NResponder.add();
           
            // This is the instantiated mode.
            var responder:NResponder;
            responder = new NResponder();
            // Or even
            responder = NResponder.add();
        }
    }
}
</pre>
<p>You could asking: &#8220;wait, why the &#8216;new&#8217; create an instance and the static call return an instance too?&#8221;</p>
<p>Well, you&#8217;ll understand soon, the &#8220;new&#8221; constructor is generic, and the static &#8220;adds&#8221; (in plural) have some shortcuts to you. As I&#8217;ve mentioned earlier, the NResponder can work with native events in Flash, so the different &#8220;adds&#8221; help you to create it. Although the &#8220;new&#8221; constructor make it, you&#8217;ll need to write more parameters to it.</p>
<p>Reasons why:<br />
The static and instantiated mode doesn&#8217;t have any impact in performance of memory of your application. The unique reason to use instantiated is if you need to change some behaviors of NResponder after creating it. Like pausing it without deleting, for example. But if you don&#8217;t need further changes, don&#8217;t worry with any instance.</p>
<p><br/></p>
<h4><strong>Creating a NResponder</strong></h4>
<p>Now let&#8217;s see how to set a call back function to an event in Flash, or more directly, how to create a NResponder. Here you&#8217;ll define your listener, will specify the Action (same as Type in EventDispatcher&#8217;s language) and all kind of things that NResponder will make for you.<br />
IMPORTANT: It&#8217;s here, in this section that you can say &#8220;Good bye&#8221; to EventDispatcher!</p>
<p>OK, the first thing that I want to show is how you normally do with EventDispatcher. When you want to use a custom event you do something like this:</p>
<pre class="brush:as3">
var myDispatcher:EventDispatcher;
myDispatcher.addEventListener('myCustomEventType',
                              callBackFunction, false, 0, true);
...
myDispatcher.dispatchEvent(new CustomEvent('myCustomEventType'));
</pre>
<p>This sucks!</p>
<p>Because the only way for you to dispatch your CustomEvent is using the &#8220;myDispatcher&#8221; instance! And look at this line of addEventListener, it is so big, completely unnecessary.<br />
Now to do the same thing with NResponder all that you need is:</p>
<pre class="brush:as3">
NResponder.add('myCustomEventType', callBackFunction);
...
NResponder.dispatch('myCustomEventType');
</pre>
<p>We&#8217;ll talk about &#8220;dispatch&#8221; later, but for now, as you can see, you don&#8217;t need to worry with additional parameters like use capture, use weak references, because NResponder doesn&#8217;t need these kind of things to work with NResponder. Here is a little difference between the EventDispatcher and NResponder. The &#8216;myCustomEventType&#8217; is the type of event to EventDispatcher, but NResponder has called it &#8220;Action&#8221;. So henceforth let&#8217;s call &#8220;Action&#8221; everything that means &#8220;Event Type&#8221; for EventDispatcher.</p>
<p>And as I&#8217;ve mentioned earlier, you can get a reference to the instance returned by the &#8220;add&#8221; method:</p>
<pre class="brush:as3">
var myReponder:NResponder = NResponder.add('myCustomEventType',
                                           callBackFunction);
</pre>
<p>Remember that this instance doesn&#8217;t change anything, it&#8217;s just for you get more control over the NResponder instance.<br />
OK, now let&#8217;s take a look at the NResponder working with native Event API. The method to create it is very similar. If you want to set a click in a button, for example, using the native Event API you probably make something like that:</p>
<pre class="brush:as3">
// probably inherited from MovieClip or Sprite.
var myButton:CustomButton;
myButton.addEventListener(MouseEvent.CLICK,
                          clickFunction, false, 0, true);
</pre>
<p>OK, now let&#8217;s translate this to NResponder&#8217;s language:</p>
<pre class="brush:as3">
// probably inherited from MovieClip or Sprite.
var myButton:CustomButton;
NResponder.addNative(myButton, MouseEvent.CLICK, clickFunction);
</pre>
<p>Just simple as that. Using the method &#8220;addNative&#8221; is very similar to &#8220;add&#8221;, with only one difference: the first parameter is the target of the native Event.</p>
<p>Some people may get confused at this point. But all that you need to remember is to ask &#8220;Who is responsible for dispatching this native event? Who is inherited from EventDispatcher class?&#8221;, so you take this object and pass it on the first parameter to &#8220;addNative&#8221; method.<br />
Like this:<br />
<img src="http://db-in.com/imgs/addnative_example.jpg" border="0"/></p>
<p>You can use NResponder to all kind of native events. The benefits is that it will work faster, more cleaner and you can use all the features of the NResponder. The following topics will explain about it.</p>
<p>Well, as you&#8217;ve noticed, as a Good Practice in programming to avoid misspelling, is advisable to you store your custom Actions in a class with &#8220;public static constants&#8221;, like this:</p>
<pre class="brush:as3">
public static const CUSTOM_ACTION:String = 'myCustomAction';
...
NResponder.add(CUSTOM_ACTION, callBackFunction);
</pre>
<p>This is just a tip, although in this article to simplify the purposes I&#8217;ll use literal strings to illustrate the examples.</p>
<p><br/></p>
<h4><strong>Dispatch and arguments</strong></h4>
<p>After creating a NResponder with the &#8220;adds&#8221; methods (add() and addNative()) you can dispatch these responders from anywhere in your application, even in different SWF files (loaded files in the same application). All that you need is to inform the &#8220;Action&#8221; you want to dispatch and the parameters that you want, if needed.<br />
Like this:</p>
<pre class="brush:as3">
// This will dispatch "myAction" with no arguments.
// So the callBack function doesn't need to expect any parameter.

NResponder.dispatch('myAction');

// This will dispatch "anotherAction"
// with the arguments 35 (Number) and "aString" (String)
// So the callBack function should expect
// these parameter (Number and String in this order).

NResponder.dispatch('anotherAction', [35.5, 'aString']);
</pre>
<p>The dispatch() method is simple, it will dispatch the specified Action to the whole application.</p>
<p>OK, so you could ask &#8220;If NResponder will dispatch an Action for anyone that has been registered in NResponder, what will it make with buttons, for example? Will it dispatch a click to all? How will it set what button to dispatch for?&#8221;.<br />
Good Point!<br />
The answer is: using the Target parameter!</p>
<p>Do you remember in addNative() that you need to specify the Target as the first parameter? It&#8217;s now that it makes the difference. If you specify a target when creating a NResponder, that target will work like a lock and this NResponder will only respond when it&#8217;s target is specified. Well, this might be a little confusing by now, but just think that NResponderCore will care about this for you. If you are using a native event, like MouseEvents, KeyboardEvents or any other, this will happens automatically during the dispatch. Just don&#8217;t worry about it.<br />
If you want, you can specify a target without using the native events. You may use this to take the benefits of this &#8220;lock&#8221; that target provides. But this is a more advanced topic and I don&#8217;t want to confuse you with this here. If you want to know more about this just check this Wiki of the project:</p>
<p><a href="http://code.google.com/p/nineveh-responder/wiki/DispatchAdvanced">http://code.google.com/p/nineveh-responder/wiki/DispatchAdvanced</a></p>
<p>Let&#8217;s move.</p>
<p><br/></p>
<h4><strong>Number of replies</strong></h4>
<p>As I&#8217;ve told you, this is one of the most interesting features to me. This lets you specify how many times you want a NResponder performs and after the very last one, it will automatically be removed and cleaned. All that you need is specify a number after the listener parameter in the &#8220;adds&#8221; methods.<br />
Like this:</p>
<pre class="brush:as3">
NResponder.add('myCustomEventType', callBackFunction, 5);
NResponder.addNative(myButton, MouseEvent.CLICK, clickButton, 2);
NResponder.addNative(this, Event.ADDED_TO_STAGE, init, 1);
</pre>
<p>The code above means that:</p>
<ul>
<li>&#8220;myCustomEventType&#8221; will only response 5 times</li>
<li>The CLICK on &#8220;myButton&#8221; will happens just 2 times</li>
<li>The &#8220;init&#8221; listener will happens just once when the target &#8220;this&#8221; be placed on the stage.</li>
</ul>
<p>You can specify how many times you want. Obviously you can&#8217;t specify negative number of times. And just for convention, the 0 (default) number means that NResponder will be performed infinitely.</p>
<p><br/></p>
<h4><strong>Delay time</strong></h4>
<p>The delay time is the minimum time necessary between two consecutive dispatches.<br />
Imagine that you have a button in your game and you don&#8217;t want the user start to click on it without stopping. You want the at least the user to wait 2.5 sec until he makes a new click. Well, all that you need is specify the next parameter after the number of replies in the &#8220;adds&#8221; methods.</p>
<pre class="brush:as3">
NResponder.addNative(myButton, MouseEvent.CLICK, clickButton, 0, 2.5);
</pre>
<p>This means that the &#8220;clickButton&#8221; listener just will happens again if the next click occurs after 2.5 sec. If the user make a click during the delay of 2.5 sec that click will be ignored. The 0 (default) means &#8220;no delay&#8221;.</p>
<p><br/></p>
<h4><strong>Has and remove</strong></h4>
<p>Just like the EventDispatcher class, NResponder provide a way for you to check what is registered inside the NResponderCore and a way to remove it. The advantage here is that you can combine and make multiple tasks at once. Let&#8217;s start with a simple case, you want to know your application has any registered Action of &#8220;myAction&#8221;, so:</p>
<pre class="brush:as3">
NResponder.has('myAction');
</pre>
<p>So simple!</p>
<p>This will return &#8220;true&#8221; if you have at least one registered NResponder with &#8220;myAction&#8221;. Now, imagine that you want to know if it has an &#8220;myAction&#8221; Action linked with a specific call back function.</p>
<pre class="brush:as3">
NResponder.has('myAction', myFunction);
</pre>
<p>This will return &#8220;true&#8221; if you have at least one registered NResponder with &#8220;myAction&#8221; as Action and &#8220;myFunction&#8221; as Listener. And at last, you can check if this is true for a specific target.</p>
<pre class="brush:as3">
NResponder.has('myAction', myFunction, myTarget);
</pre>
<p>This will return &#8220;true&#8221; if you have at least one registered NResponder with &#8220;myAction&#8221; as Action and &#8220;myFunction&#8221; as Listener and &#8220;myTarget&#8221; as Target. The funny thing here, is that you can combine those 3 parameters, leading to the following:</p>
<pre class="brush:as3">
NResponder.has('myAction', null, myTarget);
NResponder.has(null, myFunction, myTarget);
NResponder.has(null, myFunction, null);
NResponder.has(null, null, myTarget);
</pre>
<p>The four line above means:</p>
<ol>
<li>Has at least one registered NResponder with &#8220;myAction&#8221; as Action and &#8220;myTarget&#8221; as Target?</li>
<li>Has at least one registered NResponder with &#8220;myTarget&#8221; responding to &#8220;myFunction&#8221;?</li>
<li>Has at least one registered NResponder responding to &#8220;myFunction&#8221;, independently of Actions and Target?</li>
<li>Has at least one registered NResponder with &#8220;myTarget&#8221; as target, regardless of Listeners and Actions?</li>
</ol>
<p>The null means that you want to ignore that parameter and focus on the others. These combination can be very helpful in the case that you don&#8217;t have more access to one or two of those parameters. The most helpful of the combination feature is in remove method. You can remove a lot of NResponders with just one line of code. The remove() method works exactly as has() method:</p>
<pre class="brush:as3">
NResponder.remove(null, null, myTarget);
</pre>
<p>The line above means that you&#8217;ll remove all the NResponder of a specific Target (&#8220;myTarget&#8221;). Now think of a button. You can register a NResponder to the &#8220;Mouse Over&#8221; action, another one to the &#8220;Mouse Out&#8221;, another to &#8220;Mouse Down&#8221;, one to &#8220;Mouse Up&#8221; and another one &#8220;Mouse Move&#8221;. Well, 5 NResponders. And after all, when you don&#8217;t want this button anymore, you can remove all these NResponder in just one line, like this:</p>
<pre class="brush:as3">
NResponder.addNative(myButton, MouseEvent.MOUSE_OVER, overList);
NResponder.addNative(myButton, MouseEvent.MOUSE_OUT, outList);
NResponder.addNative(myButton, MouseEvent.MOUSE_DOWN, startDragList);
NResponder.addNative(myButton, MouseEvent.MOUSE_UP, stopDragList);
NResponder.addNative(myButton, MouseEvent.MOUSE_MOVE, draggingList);
...
NResponder.remove(null, null, myButton);
</pre>
<p>Remember that NResponderCore is responsible for all cleaning, and it will know exactly how to clean everything with just one line of remove() method.</p>
<p><br/></p>
<h4><strong>Benefits of instantiated mode</strong></h4>
<p>With instantiated mode you can change the order of dispatches of the same Actions, you can perform the call back function directly, you can check some attributes of that instance of NResponder, change the values of delay, number of replies and so on. I&#8217;ll not talk about all of these benefits, you can find information about they at the Wiki of Project page and at docs:</p>
<p><a href="http://code.google.com/p/nineveh-responder/w/list">http://code.google.com/p/nineveh-responder/w/list</a></p>
<p><a href="http://db-in.com/frameworks/nineveh/doc/">http://db-in.com/frameworks/nineveh/doc/</a></p>
<p>But I do want to talk here about a benefit called &#8220;paused&#8221;. This property is a boolean in which &#8220;true&#8221; indicates paused. As the name suggests, you can pause the NResponder without removing it. This can be very helpful if you want to interrupt a NResponder just for a while.</p>
<p>For example, imagine that in your application you have a 3D interface that reacts to mouse position. But if the user hits the space bar it will appear a pop-up and for the time the user stay on this pop-up you want your 3D interface to stop to reacting to mouse position. Well, all that you need is use NResponder in instantiated mode and pause it when you need.<br />
Like this:</p>
<pre class="brush:as3">
var myResponder:NResponder = NResponder.addNative(my3DInterface, MouseEvent.MOUSE_MOVE, interactFunction);
...
// When start the pop-up, use this to pause this NResponder.
myResponder.paused = true;
...
// When close the pop-up, use this to reactivate this NResponder again.
myResponder.paused = false;
</pre>
<p>Obviously you can remove and add the NResponder again using the static mode, but at this way you save CPU and memory, because pausing is much much faster than adding and removing.</p>
<p>Here I&#8217;ve presented to you some features, the most important and most used, of the NResponder Engine. Remember to check the project page to see everything about NResponder. For now, this is all that we need to say &#8220;Good Bye to EventDispatcher&#8221;. With just these features you can do almost everything with NResponder.</p>
<p>Now let&#8217;s move to some real world example!</p>
<p><br/></p>
<h2><strong>REAL WORLD example</strong></h2>
<p>Finally let&#8217;s see a real example of NResponder working. </p>
<p><br/></p>
<h4><strong>At a glance</strong></h4>
<p>In this example we will create a very simple shoot game&#8217;s routine. To show you how the features and benefits of NResponder can change how you think about your work, let&#8217;s refine a little this shoot game. Imagine that the gun in this game is an old single-action revolver, so at every shoot the hammer of this revolver needs to be pulled again and this action may take about 1.5 sec. And more, the gun just has 6 bullets, after 6 shots the user must reload the gun.<br />
First let&#8217;s imagine what we need to construct this with the native Event API, how many Booleans, timers, a lot of &#8220;if then else&#8221;, and variables we need to use to achieve this behavior. I&#8217;ll show a possible way to do this, but in the middle, before the code start to get more complex, with a lot of annoying things, we&#8217;ll change and use the NResponder.</p>
<p>Now, about the project files, is important to say that I&#8217;ve placed some two-handed comments. You know, that kind of comment block that you can toggle on/off between two codes. Like this:</p>
<pre class="brush:as3">
/*
...
/*/
The code here is activated.
//*/

---------

//*
The code here is activated.
/*/
...
//*/
</pre>
<p>So in the project files I let this kind of comment for you see the difference between the native Event API and NResponder. You can easily toggle between both adding a &#8220;/&#8221; at the first line of the comment block.</p>
<p>Oh right, let&#8217;s start!</p>
<p><br/></p>
<h4><strong>Constructing with native Event API</strong></h4>
<p>At this point I&#8217;m sure you already look at the project files. So I&#8217;ll just describe what you&#8217;ve found in FLA file:<br />
<a href="http://db-in.com/imgs/tuto_1_screen1.jpg" target="_blank"><img src="http://db-in.com/imgs/tuto_1_screen1.jpg" border="0/" height="347" width="700"/></a></p>
<p>Nothing new here, just a stage with 3 MovieClips: the shot area, the reload button and the bullets. Inside the bullets MovieClip we have a MovieClip for each bullet. All of these have instance names that we&#8217;ll use in the main class later. As you can see, in the library we export a view called VShootAnimation. It is just a view class to control the animation of the shoot using a texture atlas (poof.png).</p>
<p>Now I&#8217;ll start with the NResponderExample just using the native Event API.</p>
<pre class="brush:as3">
/**
 * NResponderExample class
 *
 * @version NResponderExample v1.0
 * @author Diney Bomfim
 */
package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
   
    /**
     * Simple shoot game to illustrate the usage of NResponder.
     */
    public dynamic class NResponderExample extends MovieClip
    {
       
    //**************************************************
    //  Protected / Private Properties
    //**************************************************
       
        private var shoots:int;
       
    //**************************************************
    //  Initialization
    //**************************************************
       
        /**
         * The initialization of NResponderExample class.
         */
        public function NResponderExample():void
        {
            if (stage)
            {
                init(null);
            }
            else
            {
                addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
            }
        }
       
    //**********************************************************************************************************
    //
    //  Protected / Private Methods
    //
    //**********************************************************************************************************
       
        /**
         * Initiate the View of the application.
         *
         * @param    e
         */
        private function init(e:Event):void
        {
            // Remove the listener
            removeEventListener(Event.ADDED_TO_STAGE, init);
           
            // Definitions
            doReload(null);
           
            // Listeners
            this.btReload.addEventListener(MouseEvent.MOUSE_DOWN, doReload, false, 0, true);
        }
       
        /**
         * Make a Shoot on the valid stage area.
         *
         * @param    e
         */
        private function doShoot(e:MouseEvent):void
        {
            trace('a Shoot');
           
            var shootAnim:VShootAnimation = new VShootAnimation();
            shootAnim.x = stage.mouseX;
            shootAnim.y = stage.mouseY;
           
            addChild(shootAnim);
           
            this.mcBullets['mcBullet' + shoots].visible = false;
           
            ++shoots;
        }
       
        /**
         * Reload the gun.
         *
         * @param    e
         */
        private function doReload(e:MouseEvent):void
        {
            shoots = 1;
           
            for (var i:int = 1; i <= 6; i++)
            {
                this.mcBullets['mcBullet' + i].visible = true;
            }
           
            // Listeners
            this.mcShootArea.addEventListener(MouseEvent.MOUSE_DOWN, doShoot, false, 0, true);
        }
    }
}
</pre>
<p>Well, the above class will be our main class of the FLA file.</p>
<ul>
<li>First in the constructor of the class (NResponderExample()), just to follow a best practice we put Event.ADDED_TO_STAGE to guarantee that no errors will happens if this file is loaded by another SWF file.</li>
<li>In the init() method we remove the previously Event.ADDED_TO_STAGE, make the first reload to the gun and add the listener of the reload button.</li>
<li>In the doReload() method we make all the bullets inside the mcBullets visible again and add the listener that will make the shoots.</li>
<li>In the doShoot() method we place our VShootAnimation view class in the mouse screen position and remove one bullet from the screen.</li>
</ul>
<p>At this point the code remains simple. You could test and see what happens.<br />
But our somewhat refined behavior is not implemented yet. The bullets doesn't has a delay to simulate the old revolver, the shoots doesn't stop after the sixth and we have a bug in the line this.mcBullets['mcBullet' + shoots].visible = false; when the shoots doesn't stop after the sixth.</p>
<p>Well, try to think what should be necessary to implement our behavior.<br />
Take some minutes if necessary looking at this code and try to implement this behavior.<br />
Remember all the problems above.<br />
To do this we would probably place a lot of "if then else" in our code, create timer checks, Booleans, oh god... really boring!</p>
<p>But let's STOP here and migrate all our events to NResponder Engine.</p>
<p><br/></p>
<h4><strong>Solving the problems with NResponder</strong></h2>
<p>Now it's the BIG POINT of this article. It's right now! It's right here!<br />
It's here that the magic happens with NResponder!<br />
Believe it or not, but to solve ALL these problems, to implement our behavior to the above code, ALL that we need is to REPLACE 3 LINES of the code and may even EXCLUDE ONE!!!</p>
<p>Just it, nothing more!</p>
<pre class="brush:as3">
/**
 * NResponderExample class
 *
 * @version NResponderExample v1.0
 * @author Diney Bomfim
 */
package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
   
    import appkit.responders.NResponder;
   
    /**
     * Simple shoot game to illustrate the usage of NResponder.
     */
    public dynamic class NResponderExample extends MovieClip
    {
       
    //**************************************************
    //  Protected / Private Properties
    //**************************************************
       
        private var shoots:int;
       
    //**************************************************
    //  Initialization
    //**************************************************
       
        /**
         * The initialization of NResponderExample class.
         */
        public function NResponderExample():void
        {
            if (stage)
            {
                init(null);
            }
            else
            {
                /*
                addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
                /*/
                NResponder.addNative(this, Event.ADDED_TO_STAGE, init, 1);
                //*/
            }
        }
       
    //**********************************************************************************************************
    //
    //  Protected / Private Methods
    //
    //**********************************************************************************************************
       
        /**
         * Initiate the View of the application.
         *
         * @param    e
         */
        private function init(e:Event):void
        {
            // Remove the listener
            //removeEventListener(Event.ADDED_TO_STAGE, init); // Don't need this any more.
           
            // Definitions
            doReload(null);
           
            // Listeners
            /*
            this.btReload.addEventListener(MouseEvent.MOUSE_DOWN, doReload, false, 0, true);
            /*/
            NResponder.addNative(this.btReload, MouseEvent.MOUSE_DOWN, doReload);
            //*/
        }
       
        /**
         * Make a Shoot on the valid stage area.
         *
         * @param    e
         */
        private function doShoot(e:MouseEvent):void
        {
            trace('a Shoot');
           
            var shootAnim:VShootAnimation = new VShootAnimation();
            shootAnim.x = stage.mouseX;
            shootAnim.y = stage.mouseY;
           
            addChild(shootAnim);
           
            this.mcBullets['mcBullet' + shoots].visible = false;
           
            ++shoots;
        }
       
        /**
         * Reload the gun.
         *
         * @param    e
         */
        private function doReload(e:MouseEvent):void
        {
            shoots = 1;
           
            for (var i:int = 1; i <= 6; i++)
            {
                this.mcBullets['mcBullet' + i].visible = true;
            }
           
            // Listeners
            /*
            this.mcShootArea.addEventListener(MouseEvent.MOUSE_DOWN, doShoot, false, 0, true);
            /*/
            NResponder.addNative(this.mcShootArea, MouseEvent.MOUSE_DOWN, doShoot, 6, 1.5);
            //*/
        }
    }
}
</pre>
<p>As you can see, first using the feature of "number of replies" we make our Event.ADDED_TO_STAGE listener run once and automatically be excluded.</p>
<p>With the same feature we make our gun dispatch just 6 times and stop.<br />
And using the delay behavior we make our gun wait 1.5 sec between two consecutive shoots.<br />
Well, literally, you can say "Good Bye" to EventDispatcher in your application!</p>
<p><br/></p>
<h4><strong>Conclusion</strong></h4>
<p>Well, obviously that I've shown a specific example where the NResponder fits perfectly and you can think that this is just an isolated example. Just remember all the features and facilities that I've told you here, remember to check the project page and the docs to know all the power of NResponder.</p>
<p>But if you still have doubts about how helpful and powerful NResponder can be, I just want you to remember two things, just two little things:</p>
<ul>
<li>NResponder is completely independent, can dispatch from everywhere to everywhere and can carry everything with it.</li>
<li>With NResponder we can realize new things, new horizons that we didn't realize before. Perhaps, before it, we've just followed another ways.</li>
</ul>
<p>Finally, hope I've helped you and hope you try NResponder, test it, try to replace it in your own code, make some performance tests, in another words, ENJOY!<br />
Again I'll place here the links about NResponder and if you have any doubt about NResponder, just email me!</p>
<p>Project page: <a href="http://code.google.com/p/nineveh-responder/">http://code.google.com/p/nineveh-responder/</a></p>
<p>Blog page: <a href="http://www.actionscript.org/resources/admin/de/editor.php?name=wysiwyg2&amp;refresh=1522575863">http://db-in.com/blog/nineveh-framework/nresponder/</a></p>
<p>Download the NResponder: <a href="http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip">http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip</a></p>
<p>Thanks for everyone that read this article.</p>
<p>See you later!</p>
]]></content:encoded>
			<wfw:commentRss>http://db-in.com/blog/2010/11/nresponder-tutorial/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NResponder Released &#8211; The New Flash Event Engine</title>
		<link>http://db-in.com/blog/2010/08/nresponder-released-flash-event-engine/</link>
		<comments>http://db-in.com/blog/2010/08/nresponder-released-flash-event-engine/#comments</comments>
		<pubDate>Sun, 15 Aug 2010 09:17:44 +0000</pubDate>
		<dc:creator>Diney Bomfim</dc:creator>
				<category><![CDATA[ActionScript3.0]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Nineveh]]></category>

		<guid isPermaLink="false">http://db-in.com/blog/?p=309</guid>
		<description><![CDATA[I&#8217;m very glad to announce that Nineveh-Responder (NResponder) was released. Official code page: http://code.google.com/p/nineveh-responder/ NResponder is a new way to work with events in ActionScript 3.0 globally. You can cross events between different classes or even in different SWF files. With NResponder you add so much more flexibility to your application because you can changes&#8230;]]></description>
			<content:encoded><![CDATA[<p><a href="http://db-in.com/imgs/nineveh_responder.png" target="_blank"><img class="alignleft size-medium wp-image-240" style="border: 0pt none;" title="Nineveh Responder Engine" src="http://db-in.com/imgs/nineveh_responder.png" alt="Nineveh Responder Engine" width="240" height="240" /></a>I&#8217;m very glad to announce that Nineveh-Responder (NResponder) was released.</p>
<p><br/></p>
<p>Official code page:<br />
<a href="http://code.google.com/p/nineveh-responder/" target="_blank">http://code.google.com/p/nineveh-responder/</a></p>
<p><br/></p>
<p>NResponder is a new way to work with events in ActionScript 3.0 globally. You can cross events between different classes or even in different SWF files. With NResponder you add so much more flexibility to your application because you can changes the parameters &#8220;on the fly&#8221; in Run-Time. NResponder is inspired on Objective-C approach and is very oriented to performance and memory. You don&#8217;t need to create any instance if you don&#8217;t want.</p>
<p>For example, you can do:</p>
<pre class="brush:as3">
// Instead this
myObject.addEventListener('anEventType', initialize, false, 0, true);

// Use this
NResponder.add('anEventType', initialize);
</pre>
<p><br/></p>
<p><strong>FEATURES</strong><br />
Bellow some characteristics of NResponder:</p>
<ul>
<li>Work in static or instantiated mode;</li>
<li>Can specify the number of arguments to dispatch;</li>
<li>Can change these arguments &#8220;on the fly&#8221; (in Run-Time);</li>
<li>Can remove all NResponders;</li>
<li>Can specify the number of replies;</li>
<li>Can ordinate the replies;</li>
<li>Can associate each listener with one target;</li>
<li>Can have a delay to the next dispatch;</li>
<li>Can pause or resume the replies;</li>
<li>Can change all this attributes &#8220;on the fly&#8221; (in Run-Time);</li>
<li>Can join to native Event API of Adobe® Flash® and continue to use all this features;</li>
<li>Can instantiate and control each NResponder without change the others.</li>
</ul>
<p><br/></p>
<p><strong>LEARN TO USE IT</strong><br />
The NResponder engine is full documented and you can check all the docs here:<br />
<a href="http://db-in.com/frameworks/nineveh/doc" target="_blank">http://db-in.com/frameworks/nineveh/doc</a></p>
<p>Below you can watch videos tutorials with the features and real world examples of the usage of NResponder.</p>
<p>Youtube 5 chapters:<br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/dGQHxya0qeA?fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="480" height="385" src="http://www.youtube.com/v/dGQHxya0qeA?fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><br/></p>
<p><span style="font-size: large; font-weight: bold;">Download</span><br />
You can make the download of sources here:<br />
<a href="http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip" target="_blank">http://nineveh-responder.googlecode.com/files/Nineveh-Responder.zip</a></p>
<p>Or visit the official code page of the NResponder:<br />
<a href="http://code.google.com/p/nineveh-responder/" target="_blank">http://code.google.com/p/nineveh-responder/</a></p>
<p>Here is the blog page:<br />
<a href="http://db-in.com/blog/nineveh-framework/nresponder/">http://db-in.com/blog/nineveh-framework/nresponder/</a></p>
<p><br/></p>
<p>Remember to give me a feedback if you use it.</p>
<p><br/></p>
<p>See you in the next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://db-in.com/blog/2010/08/nresponder-released-flash-event-engine/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

