<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Dispatching Custom Events</title>
	<link>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/</link>
	<description>A digital supplement for the O'Reilly book</description>
	<pubDate>Sat, 04 Feb 2012 06:14:28 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Rich</title>
		<link>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-18914</link>
		<dc:creator>Rich</dc:creator>
		<pubDate>Tue, 19 Jul 2011 14:06:26 +0000</pubDate>
		<guid>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-18914</guid>
		<description>@Nazmul: I have no idea why your second option works in FD and not the first. I can't think of any reason for this to be true. Your version does two things: First, it changes the mouse event from up to down, and second, it wraps the listener function in an anonymous function first, which seems odd and hard to read. My first suspicion is that there's something wrong with this code that makes it work better using mouse down instead of mouse up. Beyond that, I have no idea. I'll check it out and post again if I find anything.</description>
		<content:encoded><![CDATA[<p>@Nazmul: I have no idea why your second option works in FD and not the first. I can&#8217;t think of any reason for this to be true. Your version does two things: First, it changes the mouse event from up to down, and second, it wraps the listener function in an anonymous function first, which seems odd and hard to read. My first suspicion is that there&#8217;s something wrong with this code that makes it work better using mouse down instead of mouse up. Beyond that, I have no idea. I&#8217;ll check it out and post again if I find anything.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nazmul Hoque</title>
		<link>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-17704</link>
		<dc:creator>Nazmul Hoque</dc:creator>
		<pubDate>Tue, 21 Jun 2011 07:53:19 +0000</pubDate>
		<guid>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-17704</guid>
		<description>I don't have FLASH IDE and I was trying your example and it worked well while I changed this line,
			_btn.addEventListener(MouseEvent.MOUSE_UP, onBtnUp, false, 0, true);
			- TO -
			_btn.addEventListener(MouseEvent.MOUSE_DOWN, function(evt:MouseEvent):void { onBtnDown(evt) }, false, 0, true);
I wonder why it does not work with the first line.

To work it without FLASH IDE (I am using FlashDevelop), I created a Slider.as file where com.las3.ui.OpenSlider is being imported.
&lt;pre class="code"&gt;
package  
{
  import flash.display.Sprite;
  import flash.events.Event;
  import org.flashdevelop.utils.FlashConnect;
  import com.las3.ui.OpenSlider;
  /**
   * ...
   * @author NIK Inc.
   */
  public class Slider extends Sprite
  {
    
    public function Slider() 
    {
      var scribbleBtn:ScribbleBtn = new ScribbleBtn();
      addChild(scribbleBtn);
      var scribbleLine:ScribbleLine = new ScribbleLine();
      addChild(scribbleLine);
      scribbleLine.x = 100;
      scribbleLine.y = 100;
      var scribbleSlider:OpenSlider = new OpenSlider(scribbleBtn, scribbleLine);
      scribbleSlider.addEventListener(Event.CHANGE, onSliderChange,false, 0, true);
    }
    
    private function onSliderChange(evt:Event):void {
      FlashConnect.trace(evt.target.percent);
    }
  }

}
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.net.FileFilter;
import flash.net.URLRequest;

class ScribbleBtn extends MovieClip {
  function ScribbleBtn() {
    graphics.beginFill(0xFF0000);
    graphics.drawCircle(0, 0, 10);
    graphics.endFill();    
  }
}

class ScribbleLine extends MovieClip {
  function ScribbleLine() {
    graphics.moveTo(0, 0);
    graphics.lineStyle(1, 0x330033);
    graphics.lineTo(100, 0);
  }
}
&lt;/pre&gt;

Thank you so much.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t have FLASH IDE and I was trying your example and it worked well while I changed this line,<br />
			_btn.addEventListener(MouseEvent.MOUSE_UP, onBtnUp, false, 0, true);<br />
			- TO -<br />
			_btn.addEventListener(MouseEvent.MOUSE_DOWN, function(evt:MouseEvent):void { onBtnDown(evt) }, false, 0, true);<br />
I wonder why it does not work with the first line.</p>
<p>To work it without FLASH IDE (I am using FlashDevelop), I created a Slider.as file where com.las3.ui.OpenSlider is being imported.</p>
<pre class="code">
package
{
  import flash.display.Sprite;
  import flash.events.Event;
  import org.flashdevelop.utils.FlashConnect;
  import com.las3.ui.OpenSlider;
  /**
   * ...
   * @author NIK Inc.
   */
  public class Slider extends Sprite
  {

    public function Slider()
    {
      var scribbleBtn:ScribbleBtn = new ScribbleBtn();
      addChild(scribbleBtn);
      var scribbleLine:ScribbleLine = new ScribbleLine();
      addChild(scribbleLine);
      scribbleLine.x = 100;
      scribbleLine.y = 100;
      var scribbleSlider:OpenSlider = new OpenSlider(scribbleBtn, scribbleLine);
      scribbleSlider.addEventListener(Event.CHANGE, onSliderChange,false, 0, true);
    }

    private function onSliderChange(evt:Event):void {
      FlashConnect.trace(evt.target.percent);
    }
  }

}
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.net.FileFilter;
import flash.net.URLRequest;

class ScribbleBtn extends MovieClip {
  function ScribbleBtn() {
    graphics.beginFill(0xFF0000);
    graphics.drawCircle(0, 0, 10);
    graphics.endFill();
  }
}

class ScribbleLine extends MovieClip {
  function ScribbleLine() {
    graphics.moveTo(0, 0);
    graphics.lineStyle(1, 0x330033);
    graphics.lineTo(100, 0);
  }
}
</pre>
<p>Thank you so much.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kasper Kamperman</title>
		<link>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8732</link>
		<dc:creator>Kasper Kamperman</dc:creator>
		<pubDate>Mon, 11 Oct 2010 15:34:17 +0000</pubDate>
		<guid>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8732</guid>
		<description>Is it true that its only possible to dispatch an event from a function that itself is called(dispatched) by an event ? I want to dispatch an event when an object is made. 

I tried the code below (without the TimerEvent) and that didn't work. Any idea why ?

&lt;pre class="code"&gt;
package 
{
  //import flash.utils.Timer;
  //import flash.events.TimerEvent;
  import flash.events.EventDispatcher;
  import flash.events.Event;
  
  public class DispatchEventExample extends EventDispatcher {
  
    //private var _timer:Timer;
  
    public function DispatchEventExample() {
      // _timer = new Timer(1000);
      //_timer.addEventListener(TimerEvent.TIMER, test,false, 0, true);
      //_timer.start();
      test();
    }
  
    //private function test(evt:TimerEvent):void {
    private function test():void {
      dispatchEvent(new Event("onQueryComplete"));
    }
    
  }

}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Is it true that its only possible to dispatch an event from a function that itself is called(dispatched) by an event ? I want to dispatch an event when an object is made. </p>
<p>I tried the code below (without the TimerEvent) and that didn&#8217;t work. Any idea why ?</p>
<pre class="code">
package
{
  //import flash.utils.Timer;
  //import flash.events.TimerEvent;
  import flash.events.EventDispatcher;
  import flash.events.Event;

  public class DispatchEventExample extends EventDispatcher {

    //private var _timer:Timer;

    public function DispatchEventExample() {
      // _timer = new Timer(1000);
      //_timer.addEventListener(TimerEvent.TIMER, test,false, 0, true);
      //_timer.start();
      test();
    }

    //private function test(evt:TimerEvent):void {
    private function test():void {
      dispatchEvent(new Event("onQueryComplete"));
    }

  }

}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rich</title>
		<link>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8026</link>
		<dc:creator>Rich</dc:creator>
		<pubDate>Thu, 12 Aug 2010 20:22:08 +0000</pubDate>
		<guid>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8026</guid>
		<description>@Mike D., Sure thing. I just noticed that I forgot to thank you for writing in. I really appreciate it. You're correct in assuming that we try to simplify things for tutorial reasons, but your point about wide adoption is a good one. It's always a challenge to figure out the best way to explain something, and we sometiems have to balance simplicity/clarity with best practices/optimization. We are, however, more flexible on the website because we're not subject to the pressure of page count restrictions imposed by publishing a full-color book. So, we'll try to think more about this kind of stuff in the future!</description>
		<content:encoded><![CDATA[<p>@Mike D., Sure thing. I just noticed that I forgot to thank you for writing in. I really appreciate it. You&#8217;re correct in assuming that we try to simplify things for tutorial reasons, but your point about wide adoption is a good one. It&#8217;s always a challenge to figure out the best way to explain something, and we sometiems have to balance simplicity/clarity with best practices/optimization. We are, however, more flexible on the website because we&#8217;re not subject to the pressure of page count restrictions imposed by publishing a full-color book. So, we&#8217;ll try to think more about this kind of stuff in the future!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike D.</title>
		<link>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8022</link>
		<dc:creator>Mike D.</dc:creator>
		<pubDate>Thu, 12 Aug 2010 18:01:25 +0000</pubDate>
		<guid>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8022</guid>
		<description>Thanks for the clarification, Rich.  That makes sense.

"I typically prefer not to include wild cards simply because, if you cite every class, you can see at a glance which classes you're using."

Agreed.</description>
		<content:encoded><![CDATA[<p>Thanks for the clarification, Rich.  That makes sense.</p>
<p>&#8220;I typically prefer not to include wild cards simply because, if you cite every class, you can see at a glance which classes you&#8217;re using.&#8221;</p>
<p>Agreed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rich</title>
		<link>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8021</link>
		<dc:creator>Rich</dc:creator>
		<pubDate>Thu, 12 Aug 2010 17:44:08 +0000</pubDate>
		<guid>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8021</guid>
		<description>@Mike, Zevan wrote this class, so I'll let him address your points. Just a quick clarification on your note about imports,though:

Using a wild card does not include a lot of classes you're not using into your project. In fact, you can look at it htis way: The import statement doesn't include any classes in your project. All the import statement does is make the classes available to the compiler. During compilation, only the classes you actually use in your code will be compiled.

I typically prefer not to include wild cards simply because, if you cite every class, you can see at a glance which classes you're using. It also may make the compilation process quicker, but I've never tested that and assume that the *average* benefit there is negligible. In any case, this has no effect of runtime performance or file size.</description>
		<content:encoded><![CDATA[<p>@Mike, Zevan wrote this class, so I&#8217;ll let him address your points. Just a quick clarification on your note about imports,though:</p>
<p>Using a wild card does not include a lot of classes you&#8217;re not using into your project. In fact, you can look at it htis way: The import statement doesn&#8217;t include any classes in your project. All the import statement does is make the classes available to the compiler. During compilation, only the classes you actually use in your code will be compiled.</p>
<p>I typically prefer not to include wild cards simply because, if you cite every class, you can see at a glance which classes you&#8217;re using. It also may make the compilation process quicker, but I&#8217;ve never tested that and assume that the *average* benefit there is negligible. In any case, this has no effect of runtime performance or file size.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike D.</title>
		<link>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8020</link>
		<dc:creator>Mike D.</dc:creator>
		<pubDate>Thu, 12 Aug 2010 16:22:08 +0000</pubDate>
		<guid>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8020</guid>
		<description>Also,

&lt;pre class="code"&gt;
import flash.events.*;
import flash.geom.*;
&lt;/pre&gt;

is not good practice.  It includes allot of classes that you are not using into your project.  You only need 4 of the classes:

&lt;pre class="code"&gt;
import flash.geom.Rectangle;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.EventDispatcher;
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Also,</p>
<pre class="code">
import flash.events.*;
import flash.geom.*;
</pre>
<p>is not good practice.  It includes allot of classes that you are not using into your project.  You only need 4 of the classes:</p>
<pre class="code">
import flash.geom.Rectangle;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.EventDispatcher;
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike D.</title>
		<link>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8019</link>
		<dc:creator>Mike D.</dc:creator>
		<pubDate>Thu, 12 Aug 2010 16:13:47 +0000</pubDate>
		<guid>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-8019</guid>
		<description>Great class!  Although, there are some thing I had to change before adding to my collection, and I suggest anyone else using this to do the same.

1.  There is no need to call onChange using an enterFrameEvent.  I changed it use the MouseMove event.
2.  In the onChange function, I check that the new percent is different, and only dispatch the CHANGE event if it is.
3.  You cast Numbers to ints in a few places.  A quick performance test shows me that this line:

     &lt;span class="inlineas"&gt;newPercent = (int(_button.x) - int(_dragArea.left)) / int(_dragArea.width);&lt;/span&gt;

takes about 8x longer to process then:

     &lt;span class="inlineas"&gt;newPercent = (_button.x - _dragArea.left) / _dragArea.width;&lt;/span&gt;

I don't see any benefit to casting those Numbers to ints, so I removed all (I think 6) casts to ints that you make.

Using this as your onChange function is much more efficient, gains in precision by not casting to ints, and fires the CHANGE event only when the button position actually changes, which is very import- you don't know how much processing a user will do on every CHANGE event, so it should happen only when it needs to:

&lt;pre class="code"&gt;
private function onChange(evt:Event):void {
     var newPercent: Number;
     if (_horizontal) {
	newPercent = (_button.x - _dragArea.left) / _dragArea.width;
     }
     else{
          newPercent = (_dragArea.bottom - _button.y) / _dragArea.height;
     }
     if (newPercent != _percent) {
          _percent = newPercent;
	  dispatchEvent(new Event(Event.CHANGE));
     }
}
&lt;/pre&gt;

4.  I changed the variable that holds the mode ("horizontal" or "vertical") to be a boolean variable named "_horizontal".  It will only ever be one of two possibilities, so there is no need to use all the extra memory to hold this as a String, and when checking the mode, Flash (and all languages) can check a boolean variable much faster then a string comparison.


I don't know if you did these things for a specific reason or just weren't thinking about performance, as this is really only meant to demonstrate the use of the dispatchEvent() function, but seeing as this has been DLed over 1500 times, I think it would be worth it to bring it up to top performance.  

I would like to know your thoughts on these points, and please correct me if I am wrong about anything I said.  These may all seem like small things, but in my experience these details accumulate in any project and can seriously hurt performance.  Besides the points I mentioned, I love the class and thank you for making it available!  It's a really good example of using dispathEvent() that can be used in real life.  I plan on using it for all my scroll bars for now on.</description>
		<content:encoded><![CDATA[<p>Great class!  Although, there are some thing I had to change before adding to my collection, and I suggest anyone else using this to do the same.</p>
<p>1.  There is no need to call onChange using an enterFrameEvent.  I changed it use the MouseMove event.<br />
2.  In the onChange function, I check that the new percent is different, and only dispatch the CHANGE event if it is.<br />
3.  You cast Numbers to ints in a few places.  A quick performance test shows me that this line:</p>
<p>     <span class="inlineas">newPercent = (int(_button.x) - int(_dragArea.left)) / int(_dragArea.width);</span></p>
<p>takes about 8x longer to process then:</p>
<p>     <span class="inlineas">newPercent = (_button.x - _dragArea.left) / _dragArea.width;</span></p>
<p>I don&#8217;t see any benefit to casting those Numbers to ints, so I removed all (I think 6) casts to ints that you make.</p>
<p>Using this as your onChange function is much more efficient, gains in precision by not casting to ints, and fires the CHANGE event only when the button position actually changes, which is very import- you don&#8217;t know how much processing a user will do on every CHANGE event, so it should happen only when it needs to:</p>
<pre class="code">
private function onChange(evt:Event):void {
     var newPercent: Number;
     if (_horizontal) {
	newPercent = (_button.x - _dragArea.left) / _dragArea.width;
     }
     else{
          newPercent = (_dragArea.bottom - _button.y) / _dragArea.height;
     }
     if (newPercent != _percent) {
          _percent = newPercent;
	  dispatchEvent(new Event(Event.CHANGE));
     }
}
</pre>
<p>4.  I changed the variable that holds the mode (&#8221;horizontal&#8221; or &#8220;vertical&#8221;) to be a boolean variable named &#8220;_horizontal&#8221;.  It will only ever be one of two possibilities, so there is no need to use all the extra memory to hold this as a String, and when checking the mode, Flash (and all languages) can check a boolean variable much faster then a string comparison.</p>
<p>I don&#8217;t know if you did these things for a specific reason or just weren&#8217;t thinking about performance, as this is really only meant to demonstrate the use of the dispatchEvent() function, but seeing as this has been DLed over 1500 times, I think it would be worth it to bring it up to top performance.  </p>
<p>I would like to know your thoughts on these points, and please correct me if I am wrong about anything I said.  These may all seem like small things, but in my experience these details accumulate in any project and can seriously hurt performance.  Besides the points I mentioned, I love the class and thank you for making it available!  It&#8217;s a really good example of using dispathEvent() that can be used in real life.  I plan on using it for all my scroll bars for now on.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rich</title>
		<link>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-5953</link>
		<dc:creator>Rich</dc:creator>
		<pubDate>Wed, 24 Feb 2010 15:46:48 +0000</pubDate>
		<guid>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-5953</guid>
		<description>@Jimfo, yes, you can pass any data type with an event. Check out the "Creating a Custom Event Class" section of the &lt;a href="/2008/11/11/passing-arguments-with-events/" rel="nofollow"&gt;Passing Arguments with Events&lt;/a&gt; post.</description>
		<content:encoded><![CDATA[<p>@Jimfo, yes, you can pass any data type with an event. Check out the &#8220;Creating a Custom Event Class&#8221; section of the <a href="/2008/11/11/passing-arguments-with-events/" rel="nofollow">Passing Arguments with Events</a> post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jimfo</title>
		<link>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-5951</link>
		<dc:creator>jimfo</dc:creator>
		<pubDate>Wed, 24 Feb 2010 15:35:18 +0000</pubDate>
		<guid>http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/#comment-5951</guid>
		<description>Is it possible to pass an object array or other data types other than strings in the event object? I have been trying to pass an array along with the event object. The eventData variable is an array. So when I try to do this then I get an implicit coercion error  can't convert array to string. I know that I can pass a string back split it up into an array using the split method of the String Class. Is there a way to pass native data types such as an array in the event object.

public override function clone():Event {
   return new CustomEvent(type, eventData, bubbles, cancelable);
}</description>
		<content:encoded><![CDATA[<p>Is it possible to pass an object array or other data types other than strings in the event object? I have been trying to pass an array along with the event object. The eventData variable is an array. So when I try to do this then I get an implicit coercion error  can&#8217;t convert array to string. I know that I can pass a string back split it up into an array using the split method of the String Class. Is there a way to pass native data types such as an array in the event object.</p>
<p>public override function clone():Event {<br />
   return new CustomEvent(type, eventData, bubbles, cancelable);<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>

