<?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>Mikko Haapoja's Blog &#187; Tips and Tricks</title>
	<atom:link href="http://www.mikkoh.com/blog/category/tips-and-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikkoh.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 31 Mar 2010 16:22:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AS3 Redirect/Throw Events Forward and Into Other Files</title>
		<link>http://www.mikkoh.com/blog/2008/11/as3-redirectthrow-events-forward-and-into-other-files/</link>
		<comments>http://www.mikkoh.com/blog/2008/11/as3-redirectthrow-events-forward-and-into-other-files/#comments</comments>
		<pubDate>Sun, 09 Nov 2008 01:01:58 +0000</pubDate>
		<dc:creator>Mikko Haapoja</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.mikkoh.com/blog/?p=82</guid>
		<description><![CDATA[This is a neat little class I wrote while working on a project with Denis at Jam3.
Basically my part was the UI for the app we were creating and we needed a lot of communication between the UI and the app so this class was one solution to the problem. (a solution that we finally [...]]]></description>
			<content:encoded><![CDATA[<p>This is a neat little class I wrote while working on a project with <strong>Denis</strong> at <strong>Jam3</strong>.</p>
<p>Basically my part was the UI for the app we were creating and we needed a lot of communication between the UI and the app so this class was one solution to the problem. (a solution that we finally didn&#8217;t end up using)</p>
<p>What this class does is <strong>listens</strong> to events and <strong>redirects/dispatches</strong> the event again.<br />
<strong>Here is the code (download at the bottom):</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.mikkoh.util
<span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">EventDispatcher</span>;
&nbsp;
  <span style="color: #3f5fbf;">/**
   * This class picks up events and redirects them to whoever is listening
   *
   * @author Mikko Haapoja
   */</span>
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> EventRedirect extends <span style="color: #004993;">EventDispatcher</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> static <span style="color: #6699cc; font-weight: bold;">var</span> instance<span style="color: #000000; font-weight: bold;">:</span>EventRedirect;
    <span style="color: #0033ff; font-weight: bold;">private</span> static <span style="color: #6699cc; font-weight: bold;">var</span> instantiating<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> EventRedirect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span>instantiating<span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Error</span> <span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Use getInstance() to get an instance of EventRedirect&quot;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>EventRedirect
    <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>instance == <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        instantiating = <span style="color: #0033ff; font-weight: bold;">true</span>;
        instance = <span style="color: #0033ff; font-weight: bold;">new</span> EventRedirect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
&nbsp;
      <span style="color: #0033ff; font-weight: bold;">return</span> instance;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> addRedirect<span style="color: #000000;">&#40;</span>itemToListenTo<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">EventDispatcher</span>, event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
    <span style="color: #000000;">&#123;</span>
      itemToListenTo.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>event, <span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><span id="more-82"></span>The meat of this Class is all in the function <strong>addRedirect</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> addRedirect<span style="color: #000000;">&#40;</span>itemToListenTo<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">EventDispatcher</span>, event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
  itemToListenTo.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>event, <span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You pass this function anything that can dispatch events (everything that dispatches events extends EventDispatcher ie. Sprite, MovieClip, Stage) and a String for the event you want the event dispatcher to listen to (ie. MouseEvent.CLICK, Event.ENTER_FRAME). This function then adds an event listener to the <strong>itemToListenTo</strong> that just dispatches/redirects the event. I took a bit of a shortcut and made the <strong>dispatchEvent</strong> function listen to the events being sent from <strong>itemToListenTo</strong> instead of writing another function that would then dispatch the event anyway.</p>
<p>The rest of this class is just a <strong>Singleton</strong> implementation. For you who do not know what a Singleton is, a Singleton is a type of class that you can only have one of. (I know I always thought what can I even use that for) I did this to ensure that everywhere where this class is used you will have the same instance. For example my Ui was another swf being loaded into Denis&#8217; app and I was registering to redirect events to Denis&#8217; app so we needed to make sure that Denis got the exact same instance to listen to.</p>
<p>So in short I will explain how you actually use this class.</p>
<p><strong>Within the class you want to dispatch events (ie. a button):</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//get an instance</span>
<span style="color: #6699cc; font-weight: bold;">var</span> redirect<span style="color: #000000; font-weight: bold;">:</span>EventRedirect=EventRedirect.getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//this is this button and MouseEvent.CLICK is</span>
<span style="color: #009900;">//the event we want to dispatch everywhere</span>
redirect.addRedirect<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p><strong>In classes we want to listen to events:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//get an instance</span>
<span style="color: #6699cc; font-weight: bold;">var</span> redirect<span style="color: #000000; font-weight: bold;">:</span>EventRedirect=EventRedirect.getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//doSomething is the function that will pick up the</span>
<span style="color: #009900;">//events being dispatched from btn</span>
redirect.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, doSomething<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Now this class is very powerful but also very dangerous which is why we didn&#8217;t end up using this in our project. I could see this class being very useful for smaller projects but could be very scary for very large projects. On a large project you could have 100&#8217;s of events being redirected everywhere and so it may get very annoying sorting out who is passing a <strong>MouseEvent.CLICK</strong> event and what should be done. The other option would be to create very specific user defined events and I think this would be a better option. Something also to keep in mind is that the more events a class is listening to the slower it will be. So if the re-directer is listening to 100&#8217;s of events it may become a bottle neck within your app.</p>
<p><strong><a href="http://www.mikkoh.com/blog/wp-content/uploads/2008/11/eventredirect.zip">Click here to download the source and a silly example use of this class</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikkoh.com/blog/2008/11/as3-redirectthrow-events-forward-and-into-other-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
