• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

AS3控制键盘的方向键代码

OC/C/C++ 水墨上仙 2682次浏览

AS3控制键盘的方向键代码

package  {
 
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
 
 
	public class KeyboardController extends MovieClip {
		private var upArrow:Boolean;
		private var downArrow:Boolean;
		private var leftArrow:Boolean;
		private var rightArrow:Boolean;
 
		public function KeyboardController() {
 
			/*	Wait for object to be added to stage so we can reference it  */
			this.addEventListener(Event.ADDED_TO_STAGE, init);
		}
 
		private function init(event:Event):void 
		{
			/*	Add keyboard listeners to the stage   */
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
			/*	Run update every frame   */
			addEventListener(Event.ENTER_FRAME, update);
		}
 
		private function update(event:Event):void 
		{
			/* Check each arrow and do something if they are pressed */
			if(leftArrow) {
				trace('left Arrow Pressed');
			}
			if(rightArrow) {
				trace('right Arrow Pressed');
			}
			if(upArrow) {
				trace('up Arrow Pressed');
			}
			if(downArrow) {
				trace('down Arrow Pressed');
			}
		}
 
		private function handleSpacebar():void
		{
			/*	Do spacebar action */
			trace("Spacebar hit");
		}
 
		private function keyPressed(event:KeyboardEvent):void 
		{
			/*	Use a switch to determine which key was pressed	*/
			var code:uint = event.keyCode;
			switch(code) 
			{
				case 37:
					leftArrow = true;
					break;
				case 38:
					upArrow = true;
					break;
				case 39:
					rightArrow = true;
					break;
				case 40:
					downArrow = true;
					break;
				case 32:
					handleSpacebar();
					break;
			}
		}
 
		private function keyReleased(event:KeyboardEvent):void 
		{
			/*	Use a switch to determine which key was released	*/
			var code:uint = event.keyCode;
			switch(code) 
			{
				case 37:
					leftArrow = false;
					break;
				case 38:
					upArrow = false;
					break;
				case 39:
					rightArrow = false;
					break;
				case 40:
					downArrow = false;
					break;
			}
		}
	}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明AS3控制键盘的方向键代码
喜欢 (0)
加载中……