Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: gravity in 2d game

1. ## gravity in 2d game

when user hit up key, jump get set to true and when user let go of up key jump get set to false.
gravity = 2
velocity = 20
y = y postion of my player

problem:

1st - if i hold up key my player go up 20 pics, but when i keep on going down for ever.
2nd - if i hit up key and let go, then hit up key and let go, and so on ..... it keep on going up for ever.

```while(true){
if(jump == true){
velocity -= gravity;
y -= velocity;
}
else if(jump == false){
velocity = 20;
}
}```

problem #2 is bc i am seting velocity = 20 in else if. to fix this problem i can just del the statment.
problem #1 is really hard and i cant find a good sloution for it. may be if i have a test inside of
` if(jump == true){`

2. ## Re: gravity in 2d game

And what happened when you tried that?

By the way, putting your game logic inside an infinite loop like that probably isn't a good idea. It's better to use a Timer.