-2
\$\begingroup\$

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]; 
	}

	@Override
	public void keyPressed(KeyEvent e) {
		keys[e.getKeyCode()] = true;
		System.out.println("Pressed!");
	}

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

	@Override
	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{
	
	//Animations
	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;
		
		//Animations
		animDino = new Animation(100, Assets.DerpDino);
	}

	@Override
	public void tick() {
		//Animations
		animDino.tick();
		//Movement
		getInput();
		move();
		handler.getGameCamera().centerOnEntity(this);
	}
	
	private void getInput(){
		xMove = 0;
		yMove = 0;
		
		if(handler.getKeyManager().up)
			yMove = -speed;
		if(handler.getKeyManager().down)
			yMove = speed;
		if(handler.getKeyManager().left)
			xMove = -speed;
		if(handler.getKeyManager().right)
			xMove = speed;
//		if(handler.getKeyManager().jump)
			//what to type here???
	}

	@Override
	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();
	}
	
}

\$\endgroup\$
4
  • 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\$ – SreeLegend 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
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.