首页 > 代码库 > AS3键盘控制

AS3键盘控制

第一种方式:

package com.game.keymanager {        import flash.display.Stage;        import flash.events.KeyboardEvent;        import flash.ui.Keyboard;        import flash.utils.Proxy;         import flash.utils.flash_proxy;          /**         * The KeyObject class recreates functionality of        * Key.isDown of ActionScript 1 and 2         *        * Usage:         * var key:KeyObject = new KeyObject(stage);         * if (key.isDown(key.LEFT)) { ... }          */        dynamic public class KeyObject extends Proxy {              private static var stage:Stage;            private static var keysDown:Object;           public function KeyObject(stage:Stage) {                construct(stage);            }             public function construct(stage:Stage):void {                 KeyObject.stage = stage;                 keysDown = new Object();                 stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);                 stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);              }                        flash_proxy override function getProperty(name:*):* {                  return (name in Keyboard) ? Keyboard [name] : -1;              }              public function isDown(keyCode:uint):Boolean {                  return Boolean(keyCode in keysDown);              }              public function deconstruct():void {                  stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);                  stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);                  keysDown = new Object();                  KeyObject.stage = null;              }              private function keyPressed(evt:KeyboardEvent):void {                keysDown [evt.keyCode] = true;              }                         private function keyReleased(evt:KeyboardEvent):void {                delete keysDown [evt.keyCode];              }          }      }   
 package com.game.keymanager {         import flash.display.Stage;     import flash.events.KeyboardEvent;     import flash.ui.Keyboard;     import flash.utils.Proxy;     import flash.utils.flash_proxy;          /**      * The KeyObject class recreates functionality of      * Key.isDown of ActionScript 1 and 2      *      * Usage:      * var key:KeyObject = new KeyObject(stage);      * if (key.isDown(key.LEFT)) { ... }      */     dynamic public class KeyObject extends Proxy {                  private static var stage:Stage;         private static var keysDown:Object;                  public function KeyObject(stage:Stage) {             construct(stage);         }                  public function construct(stage:Stage):void {             KeyObject.stage = stage;             keysDown = new Object();             stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);             stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);         }                  flash_proxy override function getProperty(name:*):* {             return (name in Keyboard) ? Keyboard[name] : -1;         }                  public function isDown(keyCode:uint):Boolean {             return Boolean(keyCode in keysDown);         }                  public function deconstruct():void {             stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);             stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);             keysDown = new Object();             KeyObject.stage = null;         }                  private function keyPressed(evt:KeyboardEvent):void {             keysDown[evt.keyCode] = true;         }                  private function keyReleased(evt:KeyboardEvent):void {             delete keysDown[evt.keyCode];         }     } } 

 暂时不多写了,因为csdn博客出问题,经常写文章都出错。真郁闷,先发一个键盘控制的类。可以进行使用的,在网上看到的。有兴趣尝试一下。用法很简单

第二种方式:使用了一种比较简洁的方式,同样是收集网上一种写法,感觉这种做法也是可以

使用的方法 使用 if(KEY.isDown(键值))这样就可以实现控制到我们希望的对象了。

 

package com.hero.ImageManager      {          /*键盘按键判断类.         使用时先初始化要监听的对象.再判断哪个键被按下了.         KEY.init(stage);         KEY.isDown(40);返回true或false         */         import flash.events.Event;          import flash.events.KeyboardEvent;          import flash.display.DisplayObject;          public class KEY {              private static  var keyObj:KEY = null;              private static  var keys:Object;              public static function init(_stage:DisplayObject):void {                  if (keyObj == null) {                      keys = {};                      _stage.addEventListener(KeyboardEvent.KEY_DOWN, KEY.keyDownHandler);                      _stage.addEventListener(KeyboardEvent.KEY_UP, KEY.keyUpHandler);                  }              }              public static function isDown( keyCode:uint ):Boolean {                  return keys [keyCode];                     }              private static function keyDownHandler( e:KeyboardEvent ):void {                  keys [e.keyCode] = true;                  trace( keys [e.keyCode]);              }              private static function keyUpHandler( e:KeyboardEvent ):void {                 delete keys [e.keyCode];             }        }    }    

 

 package com.hero.ImageManager {     /*键盘按键判断类.     使用时先初始化要监听的对象.再判断哪个键被按下了.     KEY.init(stage);     KEY.isDown(40);返回true或false     */     import flash.events.Event;     import flash.events.KeyboardEvent;     import flash.display.DisplayObject;     public class KEY {         private static  var keyObj:KEY = null;         private static  var keys:Object;         public static function init(_stage:DisplayObject):void {             if (keyObj == null) {                 keys = {};                 _stage.addEventListener(KeyboardEvent.KEY_DOWN, KEY.keyDownHandler);                 _stage.addEventListener(KeyboardEvent.KEY_UP, KEY.keyUpHandler);             }         }         public static function isDown( keyCode:uint ):Boolean {             return keys[keyCode];                       }         private static function keyDownHandler( e:KeyboardEvent ):void {             keys[e.keyCode] = true;             trace( keys[e.keyCode]);         }         private static function keyUpHandler( e:KeyboardEvent ):void {             delete keys[e.keyCode];         }     }  } 

 第三种是使用:开关式的判断

 

 private var Isleft:Boolean; private var Isright:Boolean; private var Isup:Boolean; private var Isdown:Boolean; stage.addEventListener(KeyboardEvent.KEY_DOWN,keydown);stage.addEventListener(KeyboardEvent.KEY_UP,keyup);

 

 进行简单判定

private function keydown(event:Event):void            {                 if (event.keyCode==Keyboard.UP)                  {                     Isup=true;                 }                  else if (event.keyCode==Keyboard.DOWN)                  {                     Isdown=true;                }                  else if (event.keyCode==Keyboard.LEFT)                  {                     Isleft=true;                 }                  else if (event.keyCode==Keyboard.RIGHT)                  {                    Isright=true;                                   }            }              private function keyup(event:Event):void             {                 if (event.keyCode==Keyboard.UP)                  {                    Isup=false;                 }                  else if (event.keyCode==Keyboard.DOWN)                  {                     Isdown=false;                 }                  else if (event.keyCode==Keyboard.LEFT)                  {                     Isleft=false;                  }                  else if (event.keyCode==Keyboard.RIGHT)                  {                    Isright=false;                }              }     private function keydown(event:Event):void         {             if (event.keyCode==Keyboard.UP)             {                 Isup=true;             }             else if (event.keyCode==Keyboard.DOWN)             {                 Isdown=true;             }             else if (event.keyCode==Keyboard.LEFT)             {                  Isleft=true;             }             else if (event.keyCode==Keyboard.RIGHT)             {                 Isright=true;                                }         }         private function keyup(event:Event):void         {              if (event.keyCode==Keyboard.UP)             {                 Isup=false;              }             else if (event.keyCode==Keyboard.DOWN)             {                 Isdown=false;             }             else if (event.keyCode==Keyboard.LEFT)             {                  Isleft=false;             }             else if (event.keyCode==Keyboard.RIGHT)             {                 Isright=false;              }         } 

 通过开关式的组合方式进行组合,这种也是一种常见的方式。


通过使用上面三种方式,我们可以简化我们在键盘上的控制。我个人还比较喜欢这三种方式。

通过上面的键盘控制,还可以配合到动画进行创作。

addEventListener(Event.ENTER_FRAME,GameLoop);

好,有兴趣可以把这些使用方式记录下来组合自己的键盘控制方式。

转载自:http://wgcode.iteye.com/

 

AS3键盘控制