Jump to content

Phaser moveToObject X and Y Problem


CharlesCraft50
 Share

Recommended Posts

I have a problem in my code, I use moveToObject on the enemy to follow the player, but only the Left and Right animations is working here. I want to work both of them like a normal player like me.

//Update function:

enemy.body.velocity.x = 0;
enemy.body.velocity.y = 0;

//Up and Down animations:
if(enemy.body.velocity.y < 0) {
   enemy.animations.play('enm_up');
} else {
   enemy.animations.play('enm_down');
}

//Left and Right animations:
if(enemy.body.velocity.x < 0) {
   enemy.animations.play('enm_left');
} else {
   enemy.animations.play('enm_right');
}

 

Link to comment
Share on other sites

btw, I already fixed, Thank you for responding @squilibob, I just added some code in if statements
//&& enemy.body.velocity.y < 150 && enemy.body.velocity.y > 0

if(enemy.body.velocity.x < 0 && enemy.body.velocity.y < 150 && enemy.body.velocity.y > 0) {
enemy.animations.play('enm_left');
} else if(enemy.body.velocity.x > 0 && enemy.body.velocity.y < 150 && enemy.body.velocity.y > 0) {
enemy.animations.play('enm_right');
} else if(enemy.body.velocity.y < 0) {
enemy.animations.play('enm_up');
} else if(enemy.body.velocity.y > 0) {
enemy.animations.play('enm_down');
}


 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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