i am make a 2d game in java. i have 3 class: Main.java, Player.java, Ground.java.
problem i am having is how trying to make my player to jump. i want it so it jump fast, slow down at top, and come down fast on ground. so kind of like real live.
y = player y postion
velocity_y = 10;
gravity = 2;
g.getY() = ground height
if(jump == true) //if user hit jump button
{
velocity_Y -= gravity;
y += velocity_Y; //set y
}
else if(jump == false) //if user let go of jump button
{
velocity_Y += gravity; //dont think i need this
y += velocity_Y;
if(y+30 > g.getY()) //if player hit ground(+30 for player height)
{
y = g.getY()-30; //set player y pos (-30 for height)
velocity_Y = 0; //hit ground
}
}
in this code, if user hold jump key than player keep on going up for ever. but if u let go of jump button than it does come back.