I decided to randomly start a small OpenGL 2D game, and the problem is, my keyboard input isn't working. There are no errors or anything, however, the little square I've created refuses to move. Here's my player class:
public class Player {
public int x = 0;
public int y = 0;
static int walkSpeed = 2;
private InputHandler input;
public Player() {
render();
update();
}
public void update(){
input = new InputHandler();
if (input.up) y--;
if (input.down) y++;
if (input.left) x--;
if (input.right) x++;
}
public void render() {
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
glVertex2i(300, 300);// upper left
glVertex2i(350, 300);// upper right
glVertex2i(350, 350);// bottom right
glVertex2i(300, 350);// bottom left
glEnd();
}
}
And my input handler:
public class InputHandler implements KeyListener{
private boolean[] keys = new boolean[120];
public boolean up, down, left, right, exit;
public void update(){
up = keys[KeyEvent.VK_W];
down = keys[KeyEvent.VK_S];
left = keys[KeyEvent.VK_A];
right = keys[KeyEvent.VK_D];
exit = keys[KeyEvent.VK_ESCAPE];
}
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
public void keyTyped(KeyEvent e) {
}}
I call the keylistener in the main class like this:
public class Main extends Canvas{
long lastFrame;
long fps;
long lastFps;
private InputHandler key;
public Main(){
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setTitle("OpenGL Platformer");
Display.create();
}catch (LWJGLException e){
e.printStackTrace();
}
getDelta();
lastFps = getTime();
key = new InputHandler();
addKeyListener(key);
Specifically this code adds the listener:
addKeyListener(key);
What should I do? Any help would be greatly appreciated