Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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 6059 times)
0 Members and 1 Guest are viewing this topic.
Offline thecodehunter

Junior Newbie





« Posted 2008-09-11 00: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.

Quote
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 03: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.

Heroes' Keep is in development.
Meanwhile try Bloodridge
Offline thecodehunter

Junior Newbie





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

i just want him to jump 20 pixels
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline Neo_Darkboy

Junior Newbie





« Reply #3 - Posted 2008-09-12 23: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 Member


Medals: 1
Projects: 1


Co-op Freak


« Reply #4 - Posted 2009-03-20 03: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 ->
     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.

Offline fletchergames

Senior Member





« Reply #5 - Posted 2009-03-22 14: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  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (84 views)
2013-05-17 21:29:12

alaslipknot (92 views)
2013-05-16 21:24:48

gouessej (123 views)
2013-05-16 00:53:38

gouessej (115 views)
2013-05-16 00:17:58

theagentd (127 views)
2013-05-15 15:01:13

theagentd (114 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.08 seconds with 21 queries.