0
votes

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?

1
it's not clear to me how you actually start the attack animation? (you press X and go to the idle frame then set keyAttack to `false). Please clarify the structure of your hero, looks like you have a child MovieClip in it called Attack? - BadFeelingAboutThis
myHero has 6 different keyframes which contain different animation in them. so each keyframe is triggered by a different button if that makes sense? I have the booleans set so that when the key is pressed, it is true and when it's not pressed it is false. - DJmangoLORD
I think he want something for handling animation state. Play Attack Animation -> When the attack animation end -> Play idle - Nambew

1 Answers

0
votes

One option you can do for any of the keyboard keys that you want to be pressed and released and have the animation finish is to just set the Boolean value and not tell the animation to go to the idle frame. So for the attack key only set the keyAttack boolean to false.

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

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

}

Then in your animation (MovieClip) you can have the a piece of code for at the end of the 8 frames for the attack which checks if the Boolean is false. I'm assuming that the Attack animation is inside a MovieClip called myHero from your example text above.

// At the last frame of the attack animation inside myHero.Attack
if (MovieClip(this.parent.parent).keyAttack == false){
   // Assumming that this.parent = myHero
   // and this.parent.parent = the main stage where keyAttack varable lives
    MovieClip(this.parent).gotoAndStop(1); // though I would use a frame label called ("idle")
} else {
    this.gotoAndPlay(1); // replay attack animation assuming that the X key is still pressed down
}