Jump to content

Player velocity, gravity, and animations problem


Nimgoble
 Share

Recommended Posts

Hello,

 

I'm having a little trouble with velocity and gravity in my player class's update() function.

 

I have a test level set up with a row of tiles that act as a floor for my player to collide with.  The collision is working.  But when I go to determine what animation to play in my update() function of my player class, the body.velocity.y is always non-zero.  It looks like it's the same value as what I set the body.gravity.y to.  Should I be determining my animations elsewhere? Am I doing this incorrectly?  Is there a bug?

 

The source code for my project can be viewed here: https://github.com/Nimgoble/MegaManX

 

Thanks for your help!

Link to comment
Share on other sites

if you're trying to change the animation when the character 'lands' or is standing on something, I suggest using a collision callback or the 'body.touching.down' property rather than trying to use velocity.

Link to comment
Share on other sites

I animate like this:
 

if(player.body.velocity.x > 0){player.animations.play('rightwalk');}else if(player.body.velocity.x <0){player.animations.play('leftwalk');}if(player.body.velocity.x > 0 && player.body.velocity.y > 0){player.animations.play('jumpright');}else if(player.body.velocity.x < 0 && player.body.velocity.y > 0){player.animations.play('jumpleft');}else{if(facing === 'left'){player.frame = 0;}else if(facing === 'right'){player.frame = 1;}}

Try and see.

Link to comment
Share on other sites

I animate like this:

if(player.body.velocity.x > 0)

{

player.animations.play('rightwalk');

}

else if(player.body.velocity.x <0)

{

player.animations.play('leftwalk');

}

if(player.body.velocity.x > 0 && player.body.velocity.y > 0)

{

player.animations.play('jumpright');

}

else if(player.body.velocity.x < 0 && player.body.velocity.y > 0)

{

player.animations.play('jumpleft');

}

else

{

if(facing === 'left')

{

player.frame = 0;

}

else if(facing === 'right')

{

player.frame = 1;

}

}

Try and see.

 

 

Thanks for the suggestion, Heppell.  That's what I was doing, though.  And it wouldn't work because each time I checked this.body.velocity.y, it would be something other than zero.

 

It could have something to do with the way I'm calling the collisions, though.  I have a class(TestLevel) that extends Phaser.State.  In the TestLevel update() method, I'm calling:

this.game.physics.collide(this.player, this.tiles, this.player.collisionCallback, null, this.player);

I decided to see if THAT was what was causing the issue, so I moved my animation-stuff to another method and call is immediately AFTER the above collide() call.

 

That got it to work(ish), for whatever reason.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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