0
votes

My problem here is that when i press the spacebar, it plays the attack animation but as soon as i release it it stops the animation process. I want it where i can simply press and release the button and then it completes the animation by itself.

function keyPressed(e:KeyboardEvent):void
    {
        if(e.keyCode == Keyboard.RIGHT)
          {
             rDown = true;  
          }
        if(e.keyCode == Keyboard.LEFT)
          {
             lDown = true;
          }
        if(e.keyCode == Keyboard.UP && onGround)
          {
             jumped = true;
          }
        if(e.keyCode == Keyboard.DOWN && onGround)
          {
             crouchMode = true; 
          }
        if(e.keyCode == Keyboard.SPACE && onGround)
          {
              attackMode = true;

          }
    }
    function keyReleased(e:KeyboardEvent):void
    {
        if(e.keyCode == Keyboard.RIGHT)
          {
             rDown = false; 
          }
        if(e.keyCode == Keyboard.LEFT)
          {
             lDown = false;  
          }
        if(e.keyCode == Keyboard.UP)
          {
             jumped = true;  
          }
        if(e.keyCode == Keyboard.DOWN)
          {
             crouchMode = false; 
          }
        if(e.keyCode == Keyboard.SPACE)
          {
             attackMode = false; 
          }
    }
    function gameLoop(e:Event):void
    {   
        playerStart();

              }  
        }
    }
    function playerStart():void
    {
        warMage.y += grav;//Apply gravity to player

        collisionStageCheck();

        if(crouchMode)
        {
            warMage.gotoAndStop("CrouchWarmage");
            rDown = false;
            lDown = false;
        }
        if(attackMode)
        {

            if(warMage.scaleX == 1)//If we face right
            {
              warMage.x += 5;//Lunge right
            }
            if(warMage.scaleX == -1)//If we face left
            {
              warMage.x -= 5;//Lunge left   
            }
            warMage.gotoAndPlay("AttackWarmage");
        }
        if(rDown)//If we move right
        {
           warMage.x += 12;
           warMage.gotoAndStop("RunWarmage");
           warMage.scaleX = 1;
        }
        if(lDown)//If we move left
        {
           warMage.x -= 12;
           warMage.gotoAndStop("RunWarmage");
           warMage.scaleX = -1;
        }
        if(jumped)//If we jumped
        {
           warMage.y -= 30;
           warMage.gotoAndStop("JumpWarmage");
        }
        if(!rDown && !lDown && !jumped && !crouchMode && !attackMode)//If we are in neither states
        {
            warMage.gotoAndStop("idleWarmage");
        }
    }
1
seems like it is exactly what your code should do(if(!rDown && !lDown && !jumped && !crouchMode && !attackMode) switches animation to idle), you may keep attackMode on when spacebar if released, it should help. - www0z0k
When i keep attackMode on when released, it never stops, im trying to look for another signal to say when it should be false. Like controlling yhe animation but its been pretty difficult to find anything else - user199845
try adding gotoAndStop("idleWarmage") at the last frame of AttackWarmage - www0z0k
Doesnt work :L. Is there a way to control the animation when attackMode is true so i can disable it without released the key? - user199845

1 Answers

0
votes

The attack animation is not completing because attackMode is being set to false on the spacebar keyReleased function. Don't do that. Instead, make attackMode false on the last frame of the animation. Do this in your Main class nested inside your if (attackMode) { logic block:

if (heroMage.currentFrame == "LastFrame") 
{
    attackMode = false;
}

Obviously you'd name the last frame of the attack animation "LastFrame".

I also found a more elegant approach here, using eventDispatcher which eliminates the need to check the current frame, but adds a slight nuisance (in my opinion) of having code inside a movie clip timeline.