Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 7772 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.

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 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 ->
     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 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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!