<?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; Event</title>
	<atom:link href="http://nwhy.org/tag/event/feed" rel="self" type="application/rss+xml" />
	<link>http://nwhy.org</link>
	<description>Life is short,Be yourself.</description>
	<lastBuildDate>Thu, 29 Jul 2010 02:10:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Flash-鼠标,键盘事件</title>
		<link>http://nwhy.org/flash-mouse-keyboard-event.html</link>
		<comments>http://nwhy.org/flash-mouse-keyboard-event.html#comments</comments>
		<pubDate>Thu, 21 Jan 2010 03:05:27 +0000</pubDate>
		<dc:creator>DFdou</dc:creator>
				<category><![CDATA[AIR+FB+AS3]]></category>
		<category><![CDATA[Event]]></category>

		<guid isPermaLink="false">http://nwhy.org/?p=5121</guid>
		<description><![CDATA[今天咱来看下Flash的鼠标和键盘事件。。 先放Demo: 这个Demo实现的功能很简单，鼠标放到小球上，小球透明度会变成0.5，鼠标移开会恢复到1，按住鼠标可以拖动小球。按下键盘的上下左右键可以... ]]></description>
			<content:encoded><![CDATA[<p>今天咱来看下Flash的鼠标和键盘事件。。<br />
先放Demo:<br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="600" height="500" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://nwhy.org/wp-content/uploads/2010/01/event.swf" /><embed type="application/x-shockwave-flash" width="600" height="500" src="http://nwhy.org/wp-content/uploads/2010/01/event.swf"></embed></object><br />
这个Demo实现的功能很简单，鼠标放到小球上，小球透明度会变成0.5，鼠标移开会恢复到1，按住鼠标可以拖动小球。按下键盘的上下左右键可以移动小球。<br />
下边咱慢慢来看下，显示fla文档下的代码：</p>
<pre class="brush: js">
//设置舞台属性,坐上角对齐,不拉伸
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

//new一个Role类的实例出来,并居中显示
var mRole = new Role(stage);
addChild(mRole);
setCenter(mRole);

function setCenter(obj:DisplayObject):void{
	obj.x = (stage.stageWidth - obj.width)/2;
	obj.y = (stage.stageHeight - obj.height)/2;
}

//onResize事件
stage.addEventListener(Event.RESIZE,mResize);
function mResize(e:Event):void{
	//场景大小变化时的操作
}
</pre>
<p><span id="more-5121"></span><br />
然后是这个Role类：</p>
<pre class="brush: js">
package {
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Rectangle;
	import flash.events.*;

	public class Role extends Sprite {
		private var mStage:Stage;//根场景的stage
		private var mSpeed:int = 10;//移动速度

		public function Role(stage:Stage):void {
			mStage = stage;
			//绘制形状
			draw();
			//添加事件监听
			addEventListeners();
		}
		private function draw():void {
			with (graphics) {
				beginFill(0x999999);
				lineStyle(1, 0x000000);
				drawCircle(0, 0, 10);
				endFill();
			}
		}
		private function addEventListeners():void{
			addEventListener(MouseEvent.MOUSE_OVER,mOver);//鼠标 hover
			addEventListener(MouseEvent.MOUSE_OUT,mOut);//鼠标 out
			addEventListener(MouseEvent.MOUSE_DOWN,mDown);//鼠标 down,PS:down和click是有区别的，click包含了down和up2个事件
			mStage.addEventListener(MouseEvent.MOUSE_UP,mUp);
			mStage.addEventListener(KeyboardEvent.KEY_DOWN, mKeyDown);//Keyboard事件
		}
		private function mOver(e:MouseEvent):void{
			alpha = 0.5;
		}
		private function mOut(e:MouseEvent):void{
			alpha = 1;
		}
		private function mDown(e:MouseEvent):void{
			startDrag(true,new Rectangle(0,0,mStage.stageWidth,mStage.stageHeight));
		}
		private function mUp(e:MouseEvent):void{
			stopDrag();
		}
		private function mKeyDown(e:KeyboardEvent):void{
			if (e.keyCode==37) {
				x -= mSpeed;
			} else if (e.keyCode == 39) {
				x += mSpeed;
			} else if (e.keyCode == 38) {
				y -= mSpeed;
			} else if (e.keyCode == 40) {
				y += mSpeed;
			}
			checkWall(new Rectangle(0,0,mStage.stageWidth,mStage.stageHeight));
		}
		//四周边界碰撞修正
		private function checkWall(rect:Rectangle):void{
			if(x &lt; =rect.left){
				x = rect.left;
			}
			if(x &gt;= rect.right){
				x = rect.right;
			}
			if(y &lt; =rect.top){
				y = rect.top;
			}
			if(y &gt;= rect.bottom){
				y = rect.bottom;
			}
		}
	}
}
</pre>
<h4  class="related_post_title">Some Related Posts</h4><ul class="related_post"><li>2009/04/06 -- <a  href="http://nwhy.org/as3-customevent.html" title="AS3-自定义事件攻略">AS3-自定义事件攻略</a> (2)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://nwhy.org/flash-mouse-keyboard-event.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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： package { import Customer; import Waiter; import flash.display.Sprite; import flash.t... ]]></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：</p>
<pre class="brush: js">
package {
	import Customer;
	import Waiter;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.events.MouseEvent;

	public class Barbershop extends Sprite {
		var customDF:Customer=new Customer(&quot;DFdou&quot;);
		var waiter:Waiter=new Waiter(&quot;路人A&quot;);
		public function Barbershop () {
			addChild (waiter);
			btnCutHair.addEventListener (MouseEvent.CLICK,cutHair);
			btnSpecial.addEventListener (MouseEvent.CLICK,special);
		}
		function cutHair (_evt:MouseEvent) {
			customDF.addEventListener (ServerEvent.CUT_HAIR,waiter.serve);
			customDF.callServe (ServerEvent.CUT_HAIR,&quot;MM1&quot;);
			customDF.removeEventListener (ServerEvent.CUT_HAIR,waiter.serve);
		}
		function special (_evt:MouseEvent) {
			customDF.addEventListener (ServerEvent.SPECIAL,waiter.serve);
			customDF.callServe (ServerEvent.SPECIAL,&quot;MM2&quot;);
			customDF.removeEventListener (ServerEvent.SPECIAL,waiter.serve);
		}
		public function showInfo (_str:String) {
			txtInfo.appendText (_str+&quot;\n&quot;);
		}
	}
}
</pre>
<p>自定义事件类ServerEvent.as：</p>
<pre class="brush: js">
package {
	import flash.events.Event;
	public class ServerEvent extends Event{
		public static const CUT_HAIR:String=&quot;cut_hair&quot;;
		public static const SPECIAL:String=&quot;special&quot;;
		public var _server:String;//指定个变量来存放服务人员
		public function ServerEvent (eventType:String):void {
			super(eventType);
		}
	}
}
</pre>
<p>Customer顾客类Customer.as：</p>
<pre class="brush: js">
package {
	import flash.events.EventDispatcher;
	public class Customer extends EventDispatcher {
		public var _name:String;
		public function Customer (name:String):void {
			_name=name;
		}
		public function callServe(eventType:String,server:String){
			var serverEvent:ServerEvent=new ServerEvent(eventType);
			serverEvent._server=server;//指定服务的人
			dispatchEvent(serverEvent);//发送事件
		}
	}
}
</pre>
<p>Waiter类Waiter.as：</p>
<pre class="brush: js">
package {
	import flash.display.Sprite;
	public class Waiter extends Sprite{
		public var _name:String;
		public function Waiter (name:String) {
			_name=name;
		}
		public function serve (_evt:ServerEvent) {
			//调用root里的函数，这里是用来做Demo用到～
			Object(root).showInfo(&quot;你需要&quot;+_evt._server+&quot;服务,服务类型为:&quot;+_evt.type);
		}
	}
}
</pre>
<p>自定义事件的具体流程：<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>
