Jump to content

Attacking with a sword like older Zelda games


v33dub
 Share

Recommended Posts

Hey all - I'm new to Phaser and I have been searching for days on how to make a sword attack like in older Zelda games. I have it to where I can play the attack animation upon hitting the spacebar, but then I have no idea how to get the sprite back to the idle animation. Could anyone point me in the right direction as to how I might go about learning to do this? Thanks!

Link to comment
Share on other sites

Hi, @v33dub!

You can use a Timer object to control the duration of the attack movement, and to control which animation to play:

1. Player presses the attack button which calls the "attack" function;

2. Set the animation to "attacking", and trigger the Phaser.Timer object to last 1 second or so (depends on how fast is the attack movement);

3. You set the "idle" animation again in the function that is called when the timer ends.

The code would be something like this:

attack(){
    //We use a boolean var to check if the player is currently attacking to prevent a new attack mid animation.
    //(May not be necessary in your game.)
    if (!this.isAttacking){

        //Play the "attack" animation
        this.animations.play('attack');
        this.isAttacking = true;
        
        //Start the Timer object that will wait for 1 second and then will triger the inner function.
        this.game.time.events.add(Phaser.Timer.SECOND * 1, function(){
            this.animations.play('idle');//Returns the animation to "idle"
            this.isAttacking = false;//Returns the boolean var to "false"
        }, this);
    }
}

Let me know if it works for you!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...