Hey guys I'm working in a game where I want a jump function and gravity function instead of my player just flying around in the air. I added the function of able to press 'Space Bar' but haven't assigned any values to it and I don't know how.

Basically all I want is a simple jump function and gravity function.

This code below is my Key Manager(my keyboard inputs)

package com.vescorspel.game.MyFirstGame.input;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyManager implements KeyListener{
	private boolean[] keys;
	public boolean up, down, left, right, jump;
	public KeyManager(){
		keys = new boolean[256];
	public void tick(){
		up = keys[KeyEvent.VK_W];
		down = keys[KeyEvent.VK_S];
		left = keys[KeyEvent.VK_A];
		right = keys[KeyEvent.VK_D];
		jump = keys[KeyEvent.VK_SPACE]; 

	public void keyPressed(KeyEvent e) {
		keys[e.getKeyCode()] = true;

	public void keyReleased(KeyEvent e) {
		keys[e.getKeyCode()] = false;

	public void keyTyped(KeyEvent arg0) {


My Player code for my player.

package com.vescorspel.game.MyFirstGame.entities.creatures;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import com.vescorspel.game.MyFirstGame.Handler;
import com.vescorspel.game.MyFirstGame.gfx.Animation;
import com.vescorspel.game.MyFirstGame.gfx.Assets;

public class Player extends Creatures{
	private Animation animDino;
	public Player(Handler handler, float x, float y) {
		super(handler, x, y, Creatures.DEFAULT_CREATURE_WIDTH, Creatures.DEFAULT_CREATURE_HEIGHT);
		bounds.x = 32;
		bounds.y = 32;
		bounds.width = 92;
		bounds.height = 96;
		animDino = new Animation(100, Assets.DerpDino);

	public void tick() {
	private void getInput(){
		xMove = 0;
		yMove = 0;
			yMove = -speed;
			yMove = speed;
			xMove = -speed;
			xMove = speed;
//		if(handler.getKeyManager().jump)
			//what to type here???

	public void render(Graphics g) {
		g.drawImage(getCurrentAnimationFrame(), (int) (x - handler.getGameCamera().getxOffset()), (int) (y - handler.getGameCamera().getyOffset()), width, height, null);	
//		g.setColor(Color.red);
//		g.fillRect((int) (x + bounds.x - handler.getGameCamera().getxOffset()),
//				(int) (y + bounds.y - handler.getGameCamera().getyOffset()),
//				bounds.width, bounds.height);
	private BufferedImage getCurrentAnimationFrame(){
		return animDino.getCurrentFrame();

  • 1
    \$\begingroup\$ What engine are you using? In the end, you're going to need another variable on your player, velocity, that keeps track of the players current speed. Jumping (handler.getKeyManager().jump) would change it to a positive y speed. Gravity would be decreasing the velocity.y by a constant amount every tick() (an acceleration, not based on mass) except when you're touching the ground. Basically, you need a simple physics system in your game, but depending on the engine you're using it might be there for you, I just couldn't find anything regarding com.vescorpel on the internet. \$\endgroup\$
    – Coburn
    Nov 10 '16 at 16:58
  • \$\begingroup\$ eclipse is my engine and VescorSpel is my weebly website name. \$\endgroup\$
    – Sree
    Nov 10 '16 at 17:06
  • \$\begingroup\$ Are you talking about the [eclipse Java IDE](www.eclipse.org)? In that case, this is a custom engine then? If so, you're going to need to add a physics subsystem for your engine that handles velocities, collisions, forces, etc on your objects. \$\endgroup\$
    – Coburn
    Nov 10 '16 at 17:16
  • \$\begingroup\$ If you want to discuss it more, join this room chat.stackexchange.com/rooms/48290/from-comments-of-question \$\endgroup\$
    – Coburn
    Nov 10 '16 at 17:41

Integrating physics in your game (not only gravity) is a topic that Glenn Fiedler covered in a great manner on his website : Game Physics in Games

Reading this will give you all the answers you are looking for.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .