user1816133
user1816133

Reputation:

Action Script 3. Continue animation till last frame without interrupting

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");
            }
}

Upvotes: 0

Views: 400

Answers (3)

chano
chano

Reputation: 163

The best way to not to get interrupted is to have an identifier if the attack is in motion. In this case, better have a Boolean inAttackMode(or anything) to test and see whether it is. This is based on youe example

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");
                }}
    }

then put your

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

or better yet assign identifiers to each attack like inAttackMode1 is true then other attack can not be played yet.

Upvotes: 0

tomwesolowski
tomwesolowski

Reputation: 956

In your code Hero can attack in 3 ways simultaneously, but, as far I understand, you don't want it. So why did you make three separate variables. Make one and name it 'attack'. Its value will be number of attack type. You can also create an array that maps attack's number to keycode:

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

and use it in your key_up and key_down functions. Now, you can add new attacks without create additional ifs in functions.

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() is redundant.

Upvotes: 1

Azzy Elvul
Azzy Elvul

Reputation: 1438

In startAttack you can check if some attack are in action. You may use state, or check frames to determine can new attack start.

Upvotes: 0

Related Questions