最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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"));
}
}
}
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22