Making a Sprite Jump

Can't get the logic down

Page 1 of 1

2 Replies - 7433 Views - Last Post: 11 August 2009 - 02:26 AM Rate Topic: -----

#1 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Making a Sprite Jump

Post icon  Posted 10 August 2009 - 04:40 PM

I'm trying to make a Sprite jump. Just simply go up, and come back down to where it took off from.

I have a formula coded which seems to me like it should work, but the sprite never comes back down.

Here's what I have:

package myplatformer;

import java.awt.Rectangle;
import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Character
{
	String path = "C:/Users/Zack/Desktop/webpage/mario.png";
	
	Image image;
	
	private int dx, dy, x, y, width, height;
	
	private boolean visible;
	
	public Character()
	{
		image = new ImageIcon(path).getImage();
		
		width = image.getWidth(null);
		height = image.getHeight(null);
		
		visible = true;
		
		x = 100;
		y = 230;
		
	}
	
	public void move()
	{
		x+=dx;
		
		y +=dy;
		
		
		
		
		if(x<0)
			x=0;
		
	}
	
	public int getX()
	{
		return x;
	}
	
	public int getY()
	{
		return y;
	}
	
	public Image getImage()
	{
		return image;
	}
	
	public void setVisible(boolean visible)
	{
		this.visible = visible;
	}
	
	public boolean isVisible()
	{
		return visible;
	}
	
	public Rectangle getBounds()
	{
		return new Rectangle(x,y,width,height);
	}
	
	public void keyPressed(KeyEvent e)
	{
		int key = e.getKeyCode();
		
		if(key == KeyEvent.VK_RIGHT)
			dx = 1;
		
		if(key == KeyEvent.VK_LEFT)
			dx = -1;
		
		if(key == KeyEvent.VK_SPACE)
		{

			// this code should make it jump
			final int jumph = 20;
			final int steps = 20;
			for(int i = 0; i<steps; i++)
			{
				dy = -(-1*i*i+jumph*i);
			}
			
		}
	}
	
	public void keyReleased(KeyEvent e)
	{
		int key = e.getKeyCode();
		
		if(key == KeyEvent.VK_RIGHT)
			dx = 0;
		if(key == KeyEvent.VK_LEFT)
			dx = 0;
		if(key == KeyEvent.VK_SPACE)
			dy = 0;
	}
	
	
}



Here's my logic behind this piece of the code"dy = -(-1*i*i+jumph*i);":

With that formula the y cord should go:
0
-19
-36
-51
-64
-75
-84
-91
-96
-99
-100
99
96
91
84
75
64
51
36
19
0




But it doesn't.

Also, this jump happens VERY fast. Is there a better way to do this to make the movement slower?

Is This A Good Question/Topic? 0
  • +

Replies To: Making a Sprite Jump

#2 mocker   User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: Making a Sprite Jump

Posted 11 August 2009 - 01:52 AM

I'm not really sure why you have a for loop in your keypress event. It means you go through 20 'frames' worth of updates pretty much instantly when you press space, then in your keyReleased() you set dy to 0, which leaves the character at whatever height they were.

In order to get a more realistic jump function, base it on a simple physics model. You need to adjust acceleration based on the characters action, and have the velocity (dy) update by itself based on the acceleration. Normally there is a constant acceleration down, which the ground of course prevents us from falling off screen. If you don't want to deal with constant collision checks with the ground, then you can have a variable that toggles if they are on the ground or not (isJumping)

In your Move function (or Update() function if you have one), have an acceleration variable, and decrement it by a constant you set for gravity (real gravity isn't very fun for games). Then add that acceleration to your dy. You can also check if your character landed, to disable falling in further, which depends on how your level data is setup (collision check, static y value for the 'ground' etc).

Your keypress function is really simple. It just adds a number to the characters y acceleration and returns.

There are probably other ways to fake jumping too
Was This Post Helpful? 1
  • +
  • -

#3 virgul   User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Making a Sprite Jump

Posted 11 August 2009 - 02:26 AM

dude put some print statements in there!!! Find out what is actually going on. This will help you a lot, because you will 1 see what is wrong, and 2 learn a new debugging technique!

Hint try printing just dy first, if that doesnt clear it up for you print some of the variables in the for loop

hope this helps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1