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/

时间: 2024-10-06 22:37:06

AS3键盘控制的相关文章

用键盘控制DIV && Div闪烁

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

IOS开发学习笔记-(2)键盘控制,键盘类型设置,alert 对话框

一.关闭键盘,放弃第一响应者,处理思路有两种 ① 使用文本框的 Did End on Exit 绑定事件 ② UIControl on Touch 事件 都去操作 sender 的  resignFirstResponder #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *txtUserName; @pro

纯js实现用键盘控制DIV上下左右+放大缩小与变色

//用键盘控制DIV,红色方块为键盘操作区域,您可以进行如下操作:左右控制;背景变为绿色;背景变为黄色;背景变为蓝色放大或缩小用键盘控制DIV,红色方块为键盘操作区域,您可以进行如下操作:  上:↑ 下:↓ 左:← 右:→  Ctrl + 1 : 背景变为绿色  Ctrl + 2 : 背景变为黄色  Ctrl + 3 : 背景变为蓝色  Ctrl + ↑ : 放大  Ctrl + ↓ : 缩小 <!-- Author: XiaoWen Create a file: 2017-01-10 19:0

JS实现用键盘控制DIV上下左右+放大缩小与变色

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

【Unity3D】使用鼠标键盘控制Camera视角(即时战略类游戏视角):缩进,拉远,旋转

今天写一个demo,要用到鼠标键盘控制三维视角,因此写了个脚本用于控制. 该脚本可以用于即时战略类游戏的视角,提供了缩进,拉伸,旋转.同时按住鼠标右键不放,移动鼠标可以实现第一人称视角的效果. 1 using UnityEngine; 2 using System.Collections; 3 4 public class CameraController : MonoBehaviour { 5 6 7 public float near = 20.0f; 8 public float far

键盘控制

先安装装turtlebot_teleop包: sudo apt-get install ros-indigo-joystick-drivers ros-indigo-turtlebot-teleop 先启动一个模拟的TurtleBot sudo apt-get install ros-indigo-joystick-drivers ros-indigo-turtlebot-teleop 启动RViz: rosrun rviz rviz -d `rospack find rbx1_nav`/sim

简单的使用键盘控制方块移动

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>键盘控制方块移动</title> <style> #box{ width: 50px; height: 50px; background-color: green; position: relative; left: 50px; top: 50p

Unity游戏开发的数学与物理 2 ( 通过键盘控制物体的运动 )

通过键盘控制物体的运动 实现需注意: 获取键盘相应的方法 边界检测 合力方向的速度处理 勾股定理 using UnityEngine; using System.Collections; //通过键盘控制物体的运动 public class KeyControlMotionTest : MonoBehaviour { //物体的X位置 float posX = 0; //物体的Y位置 float posY = 0; //物体在x方向上的速度 float speedX = 1; //物体在y方向上

求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

//求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字.//例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制. 1 package com.lw.HomeWork1;//包名 2 import java.util.Scanner; 3 public class Demo18 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO