I managed to get it to work using two different approaches: one is a position dependent code and the other is object state dependent. The latter being the best choice in my opinion given the fact that I am trying to make a 2D platformer in Java from scratch.
Problem: I got the character to move up but I cannot tell the character to go down without making the condition in the if statement evaluating a y coordinate of a position on screen. The reason why I do not like the position dependent code is because it looks messy but it works...for now. I have not added any platforms to the game so this code might need to be entirely object state dependent.
Is there another approach that is readable? I have a habit of writing unreadable code initially even with a lot of diagram and pseudo code planning on paper. Any suggestion on your approach or a refinement on the below code is welcomed.
if(state == ActionState.JUMPING)
{
// 315 is the y coordinate threshold on the screen. the character is on y coordinate 595.
// y coordinate decreases to this threshold and then will execute the else if
if(getY() > 315)
{
move character up
setY(getY() - jumpSpeed);
}
else
{
otherwise do this
state = ActionState.FALLING;
}
}
// will execute when character reaches the threshold
else if(state == ActionState.FALLING )
{
// temp is a copied value of the initial y coordinate when the character was created on the ground
// y coordinate
if(getY() < temp)
{
// move character down
setY(getY() + fallSpeed);
}
// at some point the character will reached its initial coordinate which is the value of temp
else
{
// so it will execute its idle animation
state = ActionState.IDLE;
}
}