0

I'm creating flash game. Here are 3 different attacks with different animations (keyboard bind z, x, c).

Problem #1

For example If I use attack1 (by clicking "z") It shows animation with ~100 frames, but If during animation I clicking attack2 (x) It cancels attack1 animation and start playing attack2 animation. I need to make that when during animation It can't be interrupted by using other animation.

Problem #2

If I use attack1 (by clicking "z") and hold "z" animation freezes till I release "z" button. I need to make that If I click any attack button once started play animation and It can't be interrupted/paused by clicking the same button.

In every attack MovieClip in last frame I added code MovieClip(this.parent).gotoAndStop("stay"); that after attack animation played It started playing "stay" animation (this part working).

key_down function:

private function key_down(event:KeyboardEvent)
{
    if (event.keyCode == 90)
    {
        attack1 = true;
    }
    if (event.keyCode == 88)
    {
        attack2 = true;
    }
    if (event.keyCode == 67)
    {
        attack3 = true;
    }

}

key_up function:

private function key_up(event:KeyboardEvent)
{

    if (event.keyCode == 90)
    {
        attack1 = false;
    }
    if (event.keyCode == 88)
    {
        attack2 = false;
    }
    if (event.keyCode == 67)
    {
        attack3 = false;
    }
}

startAttack() function

private function startAttack() {

            if (attack1)
            {
                Hero.gotoAndStop("attack1");
            }
            if (attack2)
            {
                Hero.gotoAndStop("attack2");
            }
            if (attack3)
            {
                Hero.gotoAndStop("attack3");
            }
}
4

3 回答 3

1

在您的代码中,Hero 可以同时以 3 种方式进行攻击,但据我了解,您不想要它。那么你为什么要制作三个单独的变量。制作一个并将其命名为“攻击”。它的值将是攻击类型的数量。您还可以创建一个将攻击编号映射到键码的数组:

var codes:Array = new Array(0, 90, 88, 67);
var attack:Number = 0;

并在您的 key_up 和 key_down 函数中使用它。现在,您可以添加新的攻击,而无需在函数中创建额外的 if。

key_down:

if(attack > 0) return; // don't interrupt other attack
for(var c in codes) {
  if(codes[c] == event.keyCode) {
      attack = c;
      Hero.gotoAndStop("attack" + attack);
  }
}

key_up:

for(var c in codes) {
  if(codes[c] == event.keyCode) {
      if(c == attack) attack = 0;
  }
}

startAttack() 是多余的。

于 2013-09-20T12:14:12.400 回答
0

在 startAttack 中,您可以检查是否有一些攻击在起作用。您可以使用状态或检查帧来确定是否可以开始新的攻击。

于 2013-09-20T12:06:10.210 回答
0

不被打断的最好方法是在攻击正在进行时有一个标识符。在这种情况下,最好有一个布尔 inAttackMode(或任何东西)来测试它是否是。这是基于你的例子

private function key_down(event:KeyboardEvent)
{
    if (event.keyCode == 90)
    {
        attack1 = true;
        inAttackMode = true;
    }
    if (event.keyCode == 88)
    {
        attack2 = true;
        inAttackMode = true;
    }
    if (event.keyCode == 67)
    {
        attack3 = true;
        inAttackMode = true;
    }

}


private function startAttack() {
                if(!inAttackMode){
                if (attack1)
                {
                    Hero.gotoAndStop("attack1");
                }
                if (attack2)
                {
                    Hero.gotoAndStop("attack2");
                }
                if (attack3)
                {
                    Hero.gotoAndStop("attack3");
                }}
    }

然后把你的

MovieClip(this.parent).gotoAndStop("stay");
    inAttackMode = false;

或者更好的是为每个攻击分配标识符,例如 inAttackMode1 为真,那么其他攻击还不能播放。

于 2013-09-22T07:53:26.297 回答