flash 游戏键盘控制

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);

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值