如何检测多个按键按下事件在AS3? [英] How to detect multiple key down event in as3?
问题描述
我刚开始学习AS3。
比方说,我有两个文本框在我的精灵。
我要移动文本框1,当我preSS左或右方向键,但我也想移动文本框2,当我同时文本字段1动如...的点播游戏(preSS空间,你可以拍摄导弹你移动时)。
我真的很喜欢我的发布源$ C $ C ...但我确实不知道从哪里开始。
以下code移动文本框1,当我preSS箭头键...
我的code片断:
私有函数keyHandler(事件:KeyboardEvent的):无效
{
开关(event.key code)
{
案例38:
this._txt.y - = 10;
打破;
案例40:
this._txt.y + = 10;
打破;
案例39:
this._txt.x + = 10;
打破;
案例37:
this._txt.x - = 10;
打破;
}
}
包{
进口flash.display.Sprite;
进口对象类型:flash.events.Event;
进口flash.events.KeyboardEvent;
进口flash.ui.Keyboard;
/ **
* ...
* @author www0z0k
* /
公共类KeyExample扩展Sprite {
私人变种_theyAre pressed:对象= {};
公共职能KeyExample(){
如果(阶段)的init();
其他的addEventListener(Event.ADDED_TO_STAGE,INIT);
}
私有函数初始化(五:事件):无效{
removeEventListener(Event.ADDED_TO_STAGE,INIT);
stage.addEventListener(KeyboardEvent.KEY_DOWN,onDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onUp);
}
私有函数onUp(E:的KeyboardEvent):无效{
_theyAre pressed [e.key code] = FALSE;
}
私有函数onDown(E:的KeyboardEvent):无效{
_theyAre pressed [e.key code] =真;
如果(_theyAre pressed [Keyboard.SPACE]放大器;&安培; _theyAre pressed [Keyboard.UP]){
//做任何事
}
}
}
}
但请记住,这AFAIK键盘可以处理pssed同时按键$ P $数量有限,取决于硬件
I just got started learning AS3.
Let's say I have two textfields on my sprite.
I like to move textfield 1 when I press left or right arrow keys, but I also want to move textfield 2 when I press space while textfield 1 is moving like...an airplay game (you can shoot a missile while you're moving).
I really like to post my source code...but I actually have no idea where to begin.
the following code moves textfield 1 when I press arrow keys...
my code snippet:
private function keyHandler(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case 38:
this._txt.y -= 10;
break;
case 40:
this._txt.y += 10;
break;
case 39:
this._txt.x += 10;
break;
case 37:
this._txt.x -= 10;
break;
}
}
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
/**
* ...
* @author www0z0k
*/
public class KeyExample extends Sprite {
private var _theyArePressed:Object = { };
public function KeyExample() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
}
private function onUp(e:KeyboardEvent):void {
_theyArePressed[e.keyCode] = false;
}
private function onDown(e:KeyboardEvent):void {
_theyArePressed[e.keyCode] = true;
if (_theyArePressed[Keyboard.SPACE] && _theyArePressed[Keyboard.UP]) {
//do anything
}
}
}
}
but keep in mind that that AFAIK keyboards can handle limited quantity of keys pressed at the same time, depending on the hardware
这篇关于如何检测多个按键按下事件在AS3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!