PDA

View Full Version : Translate with keyboard Input Question



opiop65
12-24-2012, 07:25 AM
So I have a simple square rendered in the immediate mode. For some reason, I cannot figure out how to actually use the translate function to make the square move. Here's my player class:



public class Player extends Canvas {

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();
addKeyListener(input);

if (input.up) System.out.print("up"); y--;
if (input.down) System.out.print("down"); y++;
if (input.left) System.out.print("left"); x--;
if (input.right) System.out.print("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 here's my input handler class:


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) {

}

}

Did I set up my key listener wrong?