0

问题是我在游戏中遇到了战斗机的问题。如果我使用 KEY_DOWN 事件和/或 ENTER_FRAME,当我按住踢或拳按钮时,战斗机会持续对敌人造成伤害,但我希望他要么踢,然后返回静止位置,要么踢并能够保持位置,但只造成一次伤害。这是一些代码:

stage.addEventListener(KeyboardEvent.KEY_DOWN, enterAttack);
stage.addEventListener(KeyboardEvent.KEY_UP, exitAttack);
stage.addEventListener(Event.ENTER_FRAME, attackYes);
stage.addEventListener(Event.EXIT_FRAME, attackNo);

function enterAttack(evt:KeyboardEvent):void
{
    if (evt.keyCode == 84)
    {
        attack = true;
    }
}
function exitAttack(evt:KeyboardEvent):void
{
    attack = false;
}
function attackYes(evt:Event):void
{
    if (attack)
    {
        hero.gotoAndStop("punch1");

        checkHitRed();
        checkIfDead();
    }
}
function attackNo(evt:Event):void
{
    if (!attack)
    {
        hero.gotoAndStop("still");
    }
}

我试图将监听器移到某个地方,但这总是让战斗机看起来没有进行任何攻击。

有什么办法可以防止按住踢/拳按钮?

任何帮助表示赞赏

4

2 回答 2

0

性格状态,分为三种。如下。

stage.addEventListener(KeyboardEvent.KEY_DOWN, enterAttack);
stage.addEventListener(KeyboardEvent.KEY_UP, exitAttack);
stage.addEventListener(Event.ENTER_FRAME, onAction);

var characterState:String = "wait"
function enterAttack(evt:KeyboardEvent):void
{
    if (evt.keyCode == 84)
    {
        characterState = "attack";
    }
}
function exitAttack(evt:KeyboardEvent):void
{
    characterState = "attack_ended";
}
function onAction(evt:Event):void
{
    if(characterState == "wait") return;

    if(characterState == "attack")
    {
        hero.gotoAndStop("punch1");

        checkHitRed();
        checkIfDead();
    }
    else if(characterState == "attack_ended")
    {
        hero.gotoAndStop("still");
        //Revert to wait state after the attack termination .
        characterState = "wait";
    }
}
于 2013-02-12T01:32:25.117 回答
0

You have to devise cooldowns for your abilities, e.g. "kick" can happen only once per 20 frames. So, when the player presses kick button, char does a kick, does damage, and sets the cooldown variable. Enter frame listener starts to decrement the counter, and subsequent kick commands are ignored while the cooldown is active.

stage.addEventListener(KeyboardEvent.KEY_DOWN, enterAttack);
stage.addEventListener(Event.ENTER_FRAME, enterFrame);
var kickCooldown:int=0;
function enterAttack(evt:KeyboardEvent):void
{
    if (evt.keyCode == 84)
    {
        if (!kickCooldown) {
            attack = true;
            kickCooldown=15;
        }
    }
}
function enterFrame(evt:Event):void
{
    if (attack)
    {
        hero.gotoAndStop("punch1");
        attack=false; // we have attacked once, now dealing damage
        checkHitRed();
        checkIfDead();
    }
    if (kickCooldown>0) kickCooldown--; // waiting for cooldown end
    else hero.gotoAndStop("still"); // if ends, return hero to "still" posture
}
于 2013-02-12T05:36:05.927 回答