CharlesCraft50 Posted November 11, 2016 Share Posted November 11, 2016 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 More sharing options...
squilibob Posted November 12, 2016 Share Posted November 12, 2016 The last line else { enemy.animations.play('enm_right'); } is being called even if the enemy velocity.x is 0 Maybe you should have another if statement there: //Left and Right animations: if(enemy.body.velocity.x < 0) { enemy.animations.play('enm_left'); } else if(enemy.body.velocity.x > 0){ enemy.animations.play('enm_right'); } CharlesCraft50 1 Link to comment Share on other sites More sharing options...
CharlesCraft50 Posted November 12, 2016 Author Share Posted November 12, 2016 @squilibobLeft and right animations is working even I remove the else if statement, my problem is Up and down animations is not working, but if I remove the Left and right animations code, The Up and Down will work, but I want to work both of them like a normal human. Link to comment Share on other sites More sharing options...
CharlesCraft50 Posted November 12, 2016 Author Share Posted November 12, 2016 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 More sharing options...
Recommended Posts