make my sprite jump

Hi all, im having a problem making my sprite jump. The thing is my sprite is standing on 400 y. When i press the spacebar, i want my character to go up and come down in the same spot without calling timer if possible. just to let you know my situation. i am using a windowed GUI calling canvas and Jframe, and JPanel. So I think that threads in my case are not an option. to alivate some confusion here a some parts of the code.

If anyone is wondering why i did it this way it’s because i had to draw and move my sprite on the screen or windowed GUI.

Everything in a game is timed - how high do you want him to jump? This is based on vertical velocity and gravity, both of which are based on time: His muscles push him upwards and gravity pulls him back down.
The equation is: s=ut+0.5att where s=distance, u=initial (upward) velocity, a=acceleration (gravity), t=time.

i just want him to jump 20 pixels

As SimonH says, you have to use the equation, you can use t= 1, so the equation only left for acceleration, who control the speed and direction of your sprite when it jumps. If the acceleration is +, then the sprite is going down, if the acceleration is -, the sprite goes up, and you can handle that in your update method, something like acc *= ((posY > jumpMAX) ? -1 : 1), and of course, validate that the change from negative to positive only occurs one time.

Anyway, hope this can help you

you guys are making it too mathematical.

think nintendo. set fps to say 24 frames per sec.
character jumps 20 pixels high in one second.

frame ->
1–12–24

20 . X .
. .
. .
. .
0 X X
-----------

so you can just force it to be at height whatever at animation frame x
f.ex
frame 1-8 + 10 height
frame 9-11 + 16 height
frame 12-13 + 20 height
frame 14-17 + 16 height
frame 18-24 + 10 height

no need to always make things work with equations, lots of the old arcade stuff is hardwired pixel by pixel.

First of all, don’t have Sprite extend Canvas. You should only have one or two Swing components in your main screen: a JFrame and a JPanel. I don’t care why you’re doing it. It’s just horribly wrong.

And KeyListener should be implemented by the JPanel for the whole screen, not by the Sprite.

Store the x and y velocity in your Sprite. When you jump, set the y velocity to -4 (or some similar number) pixels per second. Then use the velocity to update your position during each update. During these updates, use the number of seconds elapsed and the velocity to determine how far to move.

To make the jump end, you must implement gravity. To do this, just add a certain amount (for instance 1 pixel per second) to the y velocity. When you check for collisions, you can set the velocity to 0 when the sprite hits the ground so that the sprite doesn’t go through the floor.