键盘组合键

//保存按键的对象(联合数组)
var key_obj:Object = new Object  ;
function onKeyDownHandler(_evt:KeyboardEvent):void
{
    //记录当前被按下的键,用键控码也行
    key_obj[String.fromCharCode(_evt.keyCode)] = true;
}
function onKeyUpHandler(_evt:KeyboardEvent):void
{
    //键弹起,从对象中删除当前键
    key_obj[String.fromCharCode(_evt.keyCode)] = null;
}
stage.addEventListener("keyUp",onKeyUpHandler);
stage.addEventListener("keyDown",onKeyDownHandler);
function moveCir(_evt:Event):void
{
    var _sprite:Sprite = _evt.target as Sprite;
    //根据对象中记录的键被按下的情况,运行
    if (key_obj["K"])
    {
        trace("K发子弹");
    }
    if (key_obj["S"])
    {
        trace("S被按下");
    }
}
addEventListener("enterFrame",moveCir);

 

posted on 2012-06-11 15:55  602147629  阅读(208)  评论(0编辑  收藏  举报