Hello everybody! I wanna make little 2D quest-game, i added a Hero and KeyAdapter... But!
On my Linux-notebook Hero walking not slow and not fast(like in all games).
On my Windows-PC Hero walking very fast. 1 keyPress on PC = 10 keyPress on notebook. How can i fix it? Thanks for help.
Code of KeyInputHandler:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package code;
import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent;
public class KeyInputHandler extends KeyAdapter {
private byte direction = 0; private boolean jumped = false;
@Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { direction = 2; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { direction = 1; } if (e.getKeyCode() == KeyEvent.VK_UP) { jumped = true; } }
@Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { direction = 0; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { direction = 0; } if (e.getKeyCode() == KeyEvent.VK_UP) { jumped = false; } } public byte getDirection(){ return direction; } public boolean getJumped(){ return jumped; } } |
Part of code in Main class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| @Override public void run() { long lastTime = System.currentTimeMillis(); long delta; try { init(); } catch (IOException ex) { Logger.getLogger(Game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } while (running) { delta = System.currentTimeMillis() - lastTime; lastTime = System.currentTimeMillis(); update(delta); render(); } } private void update(long delta) { if (KIH.getDirection() == 2) { level.walkBackward(); } if (KIH.getDirection() == 1) { level.walkForward(); } if (KIH.getJumped()){ level.heroAction(); } } |