mercredi 19 avril 2017

ActionScript3.0 Stopping character animation after button is pressed

I have a character (myHero) with several animations in him, idle, walking, turning, jump, attack, and dance. With the code I'm using now I need to keep the button pressed for the whole animation to work (it was fine for when you're walking but when i want to attack for example, i want the button to pressed once and the whole animation would play ONCE too) As of now the button needs to be continuously pressed for the animation to play. This is my code:

var keyRight:Boolean=false;
var keyLeft:Boolean=false;
var keyUp:Boolean=false;
var keyAttack:Boolean=false;
var keyDance:Boolean=false;


myHero.gotoAndStop(1);//the idle animation

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown_Handler)
function keyDown_Handler(event:KeyboardEvent):void
{

    if(event.keyCode == Keyboard.X)
    {
        keyAttack=true;
    }
//theres more code for the other keys all similar to this

}

stage.addEventListener(KeyboardEvent.KEY_UP, keyUp_Handler)
function keyUp_Handler(event:KeyboardEvent):void
{

    if(event.keyCode == Keyboard.X)
    {
        myHero.gotoAndStop(1);//the idle animation
        keyAttack=false;
    }
//theres more code for the other keys all similar to this

}

With this i have to continue having to press down the key. So I tried to adding this into the keyDown_Hander:

if(event.keyCode == Keyboard.X)
{
    if(myHero.Attack.currentFrame==8)//the animation is 8 frames long
    {
        myHero.gotoAndStop(1);//the idle animation
        keyAttack=false;
    }   
}

But now the animation loops forever when I press the key unless I cancel it with another key. I've also tried adding stop(); to the last frame of the animation but then it just freezes on the last frame and doesn't return to the idle state like I want it to.

Any help?

Aucun commentaire:

Enregistrer un commentaire