<?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>Richard Lord &#187; Events</title>
	<atom:link href="http://www.richardlord.net/blog/tag/events/feed" rel="self" type="application/rss+xml" />
	<link>http://www.richardlord.net</link>
	<description>Actionscript/Flex, PHP and Java developer</description>
	<lastBuildDate>Mon, 23 Jan 2012 17:07:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Events in Actionscript 3</title>
		<link>http://www.richardlord.net/blog/events-in-actionscript-3</link>
		<comments>http://www.richardlord.net/blog/events-in-actionscript-3#comments</comments>
		<pubDate>Wed, 15 Aug 2007 14:12:38 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Free code]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/events-in-actionscript-3/</guid>
		<description><![CDATA[<p>There was lots of positive feedback to <a href="/blog/events-in-actionscript-2/">yesterday's post</a> about my Actionscript 2 event class, so I decided to create an Actionscript 3 version. I added a lot of features while I was at it - listener priorities, preventDefault, stopNotification (aka stopImmediatePropagation), hasListeners, weak references, and to send the event as the single parameter when notifying listeners...</p>]]></description>
			<content:encoded><![CDATA[<p>There was lots of positive feedback to <a href="/blog/events-in-actionscript-2">yesterday&#8217;s post</a> about my Actionscript 2 event class, so I decided to create an Actionscript 3 version. I added a lot of features while I was at it &#8211; listener priorities, preventDefault, stopNotification (aka stopImmediatePropagation), hasListeners, weak references, and to send the event as the single parameter when notifying listeners.</p>

<p>Basically it implements almost all the features in the Actionscript 3 event model, but with all the functionallity contained within the event class itself. This way events are identified as properties of the object rather than by Strings (or by static properties containing those strings).</p>

<p>A simple example of its use would be this clicked event</p>

<pre class="code">package
{
   import bigroom.BrEvent;
   import flash.display.*;
   import flash.events.MouseEvent;
   
   class Listenable extends Sprite
   {
      public var clicked:BrEvent;
      
      public function Listenable() {
         clicked = new BrEvent( this );
         graphics.beginFill( 0xFF0000 );
         graphics.drawCircle( 50, 50, 50 );
         addEventListener( MouseEvent.MOUSE_DOWN, mouseListener );
      }
      
      public function mouseListener( ev:MouseEvent ):void {
         clicked.notify();
      }
   }
}</pre>

<pre class="code">package
{
   import flash.display.*;
   
   public class Test extends Sprite
   {
      public function Test() {
         var listen:Listenable = new Listenable();
         addChild( listen );
         listen.clicked.addListener( doClick );
      }
      
      public function doClick( ev:BrEvent ):void {
         trace( "clicked" );
      }
   }
}</pre>

<p><a href="/files/BrEvent.zip" title="Download source code">
<img src="/images/icons/asfile.gif" width="31" height="31" alt="Download source code" class="icon" />
<b>Download the BrEvent classes.</b></a></p>

<p>I first tried to implement this using entirely custom code, and everything worked fine except the weak references. I used the <a href="http://www.bigroom.co.uk/blog/create-your-own-weak-references-in-actionscript-3">weak references as described in this post</a>, and came across <a href="http://www.bigroom.co.uk/blog/different-types-of-weak-references">the problem described here</a>. This implementation is BrEvent1 in the download. It&#8217;s great if you don&#8217;t use the weak references, and interesting anyway.</p>

<p>So then I implemented it utilising the existing Event and EventDispatcher classes and that works fine. This implementation is BrEvent2 in the download. There&#8217;s a couple of redundant methods calling methods in the super class but they&#8217;re there to make the point about the overriden methods.</p>

<p>The one feature of the Actionscript 3 event model this class doesn&#8217;t implement is the capture and bubbling phases of events on display objects. The reason is, it&#8217;s not clear what events should be called in the various display objects up the chain. Unless I somehow hack the chosen events into the DisplayObject class itself, the events will only exists on selected display objects. So I didn&#8217;t implement this.</p>

<p>Having done this, I still don&#8217;t expect to use it. Although I prefer it to the built-in event handling in Actionscript 3, the built in event handling is good enough so I will use that for consistency.</p>

<p>So it was really just a short learning experience. It didn&#8217;t take long and I got to play meaninglessly with Actionscript for a morning. If you have any suggestions or you find the class(es) useful, let me know below.</p>

<p>Also, if you can think of a better name, let me know that too. I wanted to simply call the class Event but that would be too confusing with the built-in Event class.</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/events-in-actionscript-3/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Events in Actionscript 2</title>
		<link>http://www.richardlord.net/blog/events-in-actionscript-2</link>
		<comments>http://www.richardlord.net/blog/events-in-actionscript-2#comments</comments>
		<pubDate>Tue, 14 Aug 2007 13:27:49 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Free code]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/events-in-actionscript-2/</guid>
		<description><![CDATA[<p>I documented my technique for implementing event dispatching <a href="http://flashgamecode.net/free/event-class">over on the Flash Game Code site</a>. I've been using this method for a couple of years now and I think it's a lot better than EventDispatcher or ASBroadcaster. <a href="http://flashgamecode.net/free/event-class">Take a look if your interested</a>. Comments are welcome below. The source code is available for free on flashgamecode.net under the MIT licence.</p>]]></description>
			<content:encoded><![CDATA[<p>Macromedia adopted two techniques for implementing event listeners. They are the ASBroadcaster class, 
used in Flash&#8217;s intrinsic classes, and EventBroadcaster, used in all their components. I dislike both 
these techniques for a number of reasons.</p>

<ul>
<li>Both are mixins, with all the disadvantages that entails.</li>
<li>Both require the listening object to define functions with specific names in order to handle the events.</li>
<li>With ASBroadcaster, all events are broadcast to all listeners. There is no way for the listener to select only specific events to listen too.</li>
<li>With EventDispatcher, events must be specified by name as a String, which means there is no compile time checking that the event exists.</li>
</ul>

<p>So a couple of years ago I devised a new option. This technique uses a single Event class. You set-up events
on an object by creating a public property that is an Event. Other objects may then listen to this event and the
object dispatches the event when it wishes. For example</p>

<p class="code">import BigRoom.Event;

class SimpleButton
{
    public var click:Event;
	
    public function SimpleButton()
    {
        click = new Event();
    }
    
    public function onRelease():Void
    {
        click.notify( this );
    }
}</p>

<p>The notify call dispatches the event to all the listeners along with any parameters passed to the notify 
method. Listeners subscribe to the event via a call to the addListener method of the event, as in the 
following example</p>

<p class="code">class ButtonListener
{
    public function ButtonListener( btn:SimpleButton )
    {
        btn.click.addlistener( this, buttonClick );
    }
    
    public function buttonClick( obj:SimpleButton ):Void
    {
        // do something here&#8230;
    }
}</p>

<p>No mixins, no Strings, no fixed method names, and listeners subscribe to receive specific events. The
technique isn&#8217;t without its flaws, but I consider it far better than ASBroadcaster or EventDispatcher.</p>

<p><a href="/files/BigRoom_Event.zip" title="Download source code">
<img src="/images/icons/asfile.gif" width="31" height="31" alt="Download source code" class="icon" />
<b>Download the Event class.</b></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/events-in-actionscript-2/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

