I want my player to jump and i tried my best to do it and i couldn't. Basicaly my class are splitted(i have speparate player class and keyboard class) which i thought would make my life easier but didnt.
I made it so that if i press space bar, then i would fly(this is for temporarily and i wanted it to jump) and also i added gravity.
It would be great if you guys posted what to add on what class and explain what it does cause im a noob at java.
Thnx
My Player Class
package com.vescorspel.game.MyFirstGame.entities.creatures; 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 animRight, animLeft; 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 animRight = new Animation(100, Assets.DerpDino_right); animLeft = new Animation(100, Assets.DerpDino_left); } @Override public void tick() { //Animations animRight.tick(); animLeft.tick(); //Movement getInput(); move(); handler.getGameCamera().centerOnEntity(this); } private void getInput(){ xMove = 0; //Gravity yMove = 5; if(handler.getKeyManager().left) xMove = -speed; if(handler.getKeyManager().right) xMove = speed; if(handler.getKeyManager().jumping) //this makes my player fly yMove = -speed; } @Override public void render(Graphics g) { g.drawImage(getCurrentAnimationFrame(), (int) (x - handler.getGameCamera().getxOffset()), (int) (y - handler.getGameCamera().getyOffset()), width, height, null); private BufferedImage getCurrentAnimationFrame(){ if(xMove < 0){ return animLeft.getCurrentFrame(); }else{ return animRight.getCurrentFrame(); } } } My KeyManager Class
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 left, right, jumping; public KeyManager(){ keys = new boolean[256]; } public void tick(){ left = keys[KeyEvent.VK_A]; right = keys[KeyEvent.VK_D]; jumping = 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) { } }