# gravity in 2d game

• October 22nd, 2012, 10:08 PM
hwoarang69
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.

Code :

```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
Code :

` if(jump == true){`
• October 23rd, 2012, 07:40 AM
KevinWorkman
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.