一聚教程网:一个值得你收藏的教程网站

热门教程

as定义键盘按键动作

时间:2022-07-02 17:14:22 编辑:袖梨 来源:一聚教程网

/**
* 键盘控制类
* 把键盘按键动作格式化,一般用于游戏。作者:飞雨影
* 用法:Control.initStage(stage),初始化舞台;Control.addListener(function),添加listener;
* 改变控制设置:Control.changeControl(array);array为设置的keyCode
*/

package
{
        import flash.display.Stage;
        import flash.events.KeyboardEvent;
        import flash.events.EventDispatcher;
        import flash.events.Event;
       
        public class Control
        {
                 /**
                 * state为按钮状态,是否按下 isDown来得到状态
                 * controlKeyCode 为按钮控制设置数组,
                 * 默认为 w=上,s=下,a=左,d=下,u,i,j,k为四个动作按钮
                 */ 
                static private var state: Array = new Array(),controlKeyCode:Array=new Array(87,83,65,68,85,73,74,75);
                static private var controlKey:Array=new Array(),controlName:Array=new Array('上','下','左','右','轻拳','重拳','轻脚','重脚');
                static private var eventDispatcher:EventDispatcher=new EventDispatcher();
                static public function initStage( stage:Stage ): void
                {
                        var len:uint=controlKeyCode.length;
                        for(var i:uint=0;i                         {
                                controlKey[controlKeyCode[i]]=controlName[i];
                        }
                        stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
                        stage.addEventListener( KeyboardEvent.KEY_UP, onKeyUp );
                }
                static public function isDown( code: uint ): Boolean
                {
                        return state[ code ] == true;
                }
                static public function changeControl(keyCode:Array):void
                {
                        controlKeyCode = keyCode;
                }               
        static public function addListener(listener:Function):void
        {
                var len:uint=controlKeyCode.length;
                for(var i:uint=0;i                 {
                        eventDispatcher.addEventListener(controlName[i],listener);
                        eventDispatcher.addEventListener(controlName[i]+'release',listener);
                }
        }
                static private function onKeyDown( event: KeyboardEvent ): void
                {
                        var code:uint=event.keyCode
                        if(!isDown(code))
                        {
                                state[ code ] = true;
                            //trace( code,"按下");
                           if(controlKey[code])eventDispatcher.dispatchEvent(new Event(controlKey[code]));
                        }
                       
                }
                static private function onKeyUp( event: KeyboardEvent ): void
                {
                        var code:uint=event.keyCode
                                state[ event.keyCode ] = false;
                                //trace(event.keyCode,'释放')
                                if(controlKey[code])eventDispatcher.dispatchEvent(new Event(controlKey[code]+"release"));
                }

        }
}

热门栏目