Jump to content

Jumping animation not working


aspidey
 Share

Recommended Posts

Hey guys,

 

So I'm starting a new 2D game using Phaser and I'm having trouble trying to get the jumping animation to work. I'm loading my sprite from a sprite sheet and it's corresponding JSON file using the "this.load.atlasJSONHash()" function. Then, I'm adding the animations as so:

this.player.animations.add('run-left', [154, 155, 156, 157, 158, 159], 10, true);this.player.animations.add('run-right', [160, 161, 162, 163, 164, 165], 10, true);this.player.animations.add('jump-left', [120, 121, 122, 123, 124], 10, true);this.player.animations.add('jump-right', [125, 126, 127, 128, 129], 10, true);

 Once I have the animations frames added, I go to the update function and use this as my player logic:

this.game.physics.arcade.collide(this.player, this.platforms);switch(true){      // Go Left     case this.game.input.keyboard.isDown(Phaser.Keyboard.A):         this.runLeft(this.player);         // Jump while running         if(this.game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && this.player.body.touching.down){              this.pYPos = this.player.body.velocity.y = -300;              this.player.animations.play('jump-right');         }         break;     // Go right     case this.game.input.keyboard.isDown(Phaser.Keyboard.D):         this.runRight(this.player);        // Jump while running        if(this.game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && this.player.body.touching.down){            this.pYPos = this.player.body.velocity.y = -300;            this.player.animations.play('jump-left');        }        break;    // Idle jump    case (this.game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && this.player.body.touching.down):        this.pYPos = this.player.body.velocity.y = -300;        // Frame 152 is when the sprite stands and looks left        // Frame 153 is when the sprite stands and looks right        if(this.pFrame == 152){            this.player.animations.play('jump-left');        }else{        this.player.animations.play('jump-right');        }    break;    default:        this.player.body.velocity.x = 0;        this.player.animations.stop();        this.player.frame = this.pFrame;    break;}

The sprite goes left and right just fine with smooth animations and the jumping function works very well as far as physics are concernced.

 

However, I never see the jump animation. I suspect it's because I'm already in the running state and it'll still show that the sprite in the running animations even though it's jumping just fine. I can't seem to think of a good way to make the sprite go into the jumping animation. Any advice will be greatly appreciated.

 

Thanks

Link to comment
Share on other sites

My thoughts:

 

so when you press the up button, it will play the first frame in the sheet. You then release the button and the next iteration of update sets the animation to stop.

 

You don't notice this when running because you hold the button down.

 

it's gotta be something along those lines... I have a similar issue. Getting rid of the check of touching the ground helps, but obviously isn't what we want. 

Link to comment
Share on other sites

Animations override each other pretty easily; i.e. if you call play('foo') on frame 0 then call play('blah') on frame 2 it won't finish the foo animation before starting the blah animation. You have to track the last animation and only play the new one once it's finished.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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