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:
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;
/*
* 0 - no
* 1 - right
* 2 - left
*/
@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:
@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();
}
}