<?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>DFdou&#039;s Blog &#187; EventDispatcher</title>
	<atom:link href="http://nwhy.org/tag/eventdispatcher/feed" rel="self" type="application/rss+xml" />
	<link>http://nwhy.org</link>
	<description>Life is short,Be yourself.</description>
	<lastBuildDate>Wed, 08 Sep 2010 05:34:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>AS3-自定义事件攻略</title>
		<link>http://nwhy.org/as3-customevent.html</link>
		<comments>http://nwhy.org/as3-customevent.html#comments</comments>
		<pubDate>Mon, 06 Apr 2009 06:00:29 +0000</pubDate>
		<dc:creator>DFdou</dc:creator>
				<category><![CDATA[AIR+FB+AS3]]></category>
		<category><![CDATA[addEventListener()]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[EventDispatcher]]></category>

		<guid isPermaLink="false">http://nwhy.org/?p=4135</guid>
		<description><![CDATA[Demo： 这个Demo讲的是一个Customer去Barbershop消费，而Barbershop提供2种ServerEvent，CUT_HAIR和SPECIAL。 具体代码，先来文档类Barbershop.as： [js] package { import Customer; import Waiter; import flash.display.Sprite; import fl... ]]></description>
			<content:encoded><![CDATA[<p>Demo：<br />
<object id="Barbershop" height="300" width="550" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"><param name="allowScriptAccess" value="sameDomain"/><param name="movie" value="http://nwhy.org/nwhy/exp/Barbershop.swf"/><param name="quality" value="high"/><embed height="300" width="550" src="http://nwhy.org/nwhy/exp/Barbershop.swf" quality="high" bgcolor="#ffffff" name="Barbershop" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"/><br />
</object><br />
这个Demo讲的是一个Customer去Barbershop消费，而Barbershop提供2种ServerEvent，CUT_HAIR和SPECIAL。<br />
<span id="more-4135"></span><br />
具体代码，先来文档类Barbershop.as：<br />
[js]<br />
package {<br />
	import Customer;<br />
	import Waiter;<br />
	import flash.display.Sprite;<br />
	import flash.text.TextField;<br />
	import flash.events.MouseEvent;</p>
<p>	public class Barbershop extends Sprite {<br />
		var customDF:Customer=new Customer("DFdou");<br />
		var waiter:Waiter=new Waiter("路人A");<br />
		public function Barbershop () {<br />
			addChild (waiter);<br />
			btnCutHair.addEventListener (MouseEvent.CLICK,cutHair);<br />
			btnSpecial.addEventListener (MouseEvent.CLICK,special);<br />
		}<br />
		function cutHair (_evt:MouseEvent) {<br />
			customDF.addEventListener (ServerEvent.CUT_HAIR,waiter.serve);<br />
			customDF.callServe (ServerEvent.CUT_HAIR,"MM1");<br />
			customDF.removeEventListener (ServerEvent.CUT_HAIR,waiter.serve);<br />
		}<br />
		function special (_evt:MouseEvent) {<br />
			customDF.addEventListener (ServerEvent.SPECIAL,waiter.serve);<br />
			customDF.callServe (ServerEvent.SPECIAL,"MM2");<br />
			customDF.removeEventListener (ServerEvent.SPECIAL,waiter.serve);<br />
		}<br />
		public function showInfo (_str:String) {<br />
			txtInfo.appendText (_str+"\n");<br />
		}<br />
	}<br />
}<br />
[/js]<br />
自定义事件类ServerEvent.as：<br />
[js]<br />
package {<br />
	import flash.events.Event;<br />
	public class ServerEvent extends Event{<br />
		public static const CUT_HAIR:String="cut_hair";<br />
		public static const SPECIAL:String="special";<br />
		public var _server:String;//指定个变量来存放服务人员<br />
		public function ServerEvent (eventType:String):void {<br />
			super(eventType);<br />
		}<br />
	}<br />
}<br />
[/js]<br />
Customer顾客类Customer.as：<br />
[js]<br />
package {<br />
	import flash.events.EventDispatcher;<br />
	public class Customer extends EventDispatcher {<br />
		public var _name:String;<br />
		public function Customer (name:String):void {<br />
			_name=name;<br />
		}<br />
		public function callServe(eventType:String,server:String){<br />
			var serverEvent:ServerEvent=new ServerEvent(eventType);<br />
			serverEvent._server=server;//指定服务的人<br />
			dispatchEvent(serverEvent);//发送事件<br />
		}<br />
	}<br />
}<br />
[/js]<br />
Waiter类Waiter.as：<br />
[js]<br />
package {<br />
	import flash.display.Sprite;<br />
	public class Waiter extends Sprite{<br />
		public var _name:String;<br />
		public function Waiter (name:String) {<br />
			_name=name;<br />
		}<br />
		public function serve (_evt:ServerEvent) {<br />
			//调用root里的函数，这里是用来做Demo用到～<br />
			Object(root).showInfo("你需要"+_evt._server+"服务,服务类型为:"+_evt.type);<br />
		}<br />
	}<br />
}<br />
[/js]<br />
自定义事件的具体流程：<br />
1.自定义好事件类；<br />
2.注册事件侦听器；例子中为Custom callServe<br />
3.发送dispatch事件；<br />
4.侦听事件，处理；例子中为Waiter serve<br />
5.移除侦听。</p>
<h4  class="related_post_title">Some Related Posts</h4><ul class="related_post"><li>2009/05/18 -- <a  href="http://nwhy.org/as3-password-generator.html" title="AS3-无聊的密码生成器">AS3-无聊的密码生成器</a> (3)</li><li>2009/03/07 -- <a  href="http://nwhy.org/as3-addeventlistener-parameters.html" title="AS3-用addEventListener()方法的useCapture参数控制事件流">AS3-用addEventListener()方法的useCapture参数控制事件流</a> (6)</li><li>2010/03/18 -- <a  href="http://nwhy.org/as3-mapconvert.html" title="AS3-Mapppp~的一个生成方案">AS3-Mapppp~的一个生成方案</a> (3)</li><li>2010/02/04 -- <a  href="http://nwhy.org/as3-aspaceescape-map-tile.html" title="AS3-aSpaceEscape 迷宫脱离游戏(二)地图生成部分">AS3-aSpaceEscape 迷宫脱离游戏(二)地图生成部分</a> (0)</li><li>2010/02/03 -- <a  href="http://nwhy.org/as3-aspaceescape.html" title="AS3-aSpaceEscape 迷宫脱离游戏(一)分析">AS3-aSpaceEscape 迷宫脱离游戏(一)分析</a> (0)</li><li>2010/01/21 -- <a  href="http://nwhy.org/flash-mouse-keyboard-event.html" title="Flash-鼠标,键盘事件 ">Flash-鼠标,键盘事件 </a> (3)</li><li>2009/11/26 -- <a  href="http://nwhy.org/as3-caurina-tween.html" title="AS3-Caurina 动画类">AS3-Caurina 动画类</a> (1)</li><li>2009/11/24 -- <a  href="http://nwhy.org/as3-sharedobject-flash-cookie.html" title="AS3-SharedObject Flash的Cookie">AS3-SharedObject Flash的Cookie</a> (0)</li><li>2009/11/23 -- <a  href="http://nwhy.org/as3-captcha.html" title="AS3-Captcha验证码类">AS3-Captcha验证码类</a> (7)</li><li>2009/11/20 -- <a  href="http://nwhy.org/as3-bitmap-explode-effect.html" title="AS3-Effect explode 图片爆炸效果">AS3-Effect explode 图片爆炸效果</a> (2)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://nwhy.org/as3-customevent.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
