Reputation: 97
This seems like an easy-fix however I just can't seem to get it, xMove represent moving on the x-axis, y for the y-axis. Currently while my player object is colliding downwards with a tile (standing on the ground) the sprite will be facing right. I want my player to face left if I have been moving left just before that. So basically I need a way to remember what way my character is facing and return that to the yMove == 0 part of my code. Could anyone give me any advice?
private BufferedImage getCurrentAnimationFrame(){ //set Player animations when moving
if(xMove > 0){
facingRight = true;
facingLeft = false;
return animRight.getCurrentFrame();
}
if(xMove < 0){
facingLeft = true;
facingRight = false;
return animLeft.getCurrentFrame();
}
if(yMove > 0){
return Assets.playerFall;
}
if(yMove < 0){
return Assets.playerFall;
}
if(yMove == 0){
return Assets.playerFacingRight;
}
return null;
}
Edit: I tried messing around with booleans to return different sprites e.g. if(facing Left){return Assets.playerFacingLeft} however doing this somehow doesn't return an image at all.
Upvotes: 0
Views: 104
Reputation: 8575
You just need to rearrange your code: Handle the y-axis movement first. If there's no vertical movement, then check horizontal movement. I added the final if(facingLeft)
statement to handle the situation when the player is neither falling nor standing still:
private BufferedImage getCurrentAnimationFrame(){ //set Player animations when moving
if(yMove > 0){
return Assets.playerFall;
}
if(yMove < 0){
return Assets.playerFall;
}
if(xMove > 0){
facingRight = true;
facingLeft = false;
return animRight.getCurrentFrame();
}
if(xMove < 0){
facingLeft = true;
facingRight = false;
return animLeft.getCurrentFrame();
}
if(facingLeft){
return animLeft.getCurrentFrame();
} else {
return animRight.getCurrentFrame();
}
}
Upvotes: 1
Reputation:
Assuming that you considered x and y axis like this :-
if(xMove > 0){
facingRight = true;
facingLeft = false;
return animRight.getCurrentFrame();
}
if(xMove < 0){
facingLeft = true;
facingRight = false;
return Assets.playerFacingLeft; // here make the player turn left
}
if(yMove > 0){
return Assets.playerFall
}
if(yMove == 0){
return Assets.playerFacingRight;
}
if(yMove < 0){
// fall down or do nothing if you need it to do nothing you can avoid this check or set ymov back to 0
yMove = 0;
}
return null;
Upvotes: 1