Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  make my sprite jump  (Read 11016 times)
0 Members and 1 Guest are viewing this topic.
Offline thecodehunter

Junior Newbie

« Posted 2008-09-10 22:11:41 »

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.

public class MySprite extends Canvas implements KeyListener{ ...

JFrame window = new JFrame("GUI sprite");

JPanel panel = (JPanel)window.getContentPane();

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.
Offline SimonH
« Reply #1 - Posted 2008-09-11 01:30:53 »

i want my character to go up and come down in the same spot without calling timer if possible

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=u*t+0.5*a*t*t where s=distance, u=initial (upward) velocity, a=acceleration (gravity), t=time.

People make games and games make people
Offline thecodehunter

Junior Newbie

« Reply #2 - Posted 2008-09-11 01:42:50 »

i just want him to jump 20 pixels
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Neo_Darkboy

Junior Newbie

« Reply #3 - Posted 2008-09-12 21:27:38 »

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
Offline Karmington

Senior Devvie

Medals: 1
Projects: 1

Co-op Freak

« Reply #4 - Posted 2009-03-20 02:33:08 »

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 ->

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

so you can just force it to be at height whatever at animation frame x
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.

Offline fletchergames

Senior Devvie

« Reply #5 - Posted 2009-03-22 13:49:51 »

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.
Pages: [1]
  ignore  |  Print  

hadezbladez (294 views)
2018-11-16 13:46:03

hadezbladez (164 views)
2018-11-16 13:41:33

hadezbladez (295 views)
2018-11-16 13:35:35

hadezbladez (75 views)
2018-11-16 13:32:03

EgonOlsen (2156 views)
2018-06-10 19:43:48

EgonOlsen (2183 views)
2018-06-10 19:43:44

EgonOlsen (1370 views)
2018-06-10 19:43:20

DesertCoockie (1987 views)
2018-05-13 18:23:11

nelsongames (1630 views)
2018-04-24 18:15:36

nelsongames (2278 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!