0
votes

I am trying to get a simple entity/player to move when I hold down a key, but it only moves when I press it, not hold it down. I thought it was Keyboard.next() in my while loop, but that didn't work.

import static org.lwjgl.opengl.GL11.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.lwjgl.*;

public class Main {

int x = 50;
int y = 50;
int dx = 1;
int dy = 1;
long lastFrame;
double delta;

public Main() {
    try {
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.setTitle("Input Demo");
        Display.create();
    } catch(LWJGLException e) {

    }

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
    GL11.glOrtho(0, 800, 0, 600, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    while(!Display.isCloseRequested()) {
        //Render

        glClear(GL_COLOR_BUFFER_BIT);

        drawPlayer();
        movePlayer();

        Display.update();
        Display.sync(60); //60 fps

        System.out.println("LastFrame:" + lastFrame);
        System.out.println("Delta:" + delta);

    }

    Display.destroy();
}

public void movePlayer() {
    while(Keyboard.next()) {
        double delta = getDelta();
        if(Keyboard.isKeyDown(Keyboard.KEY_W)) {
            y += 3 * dy;
        } else if(Keyboard.getEventKey() == Keyboard.KEY_S && Keyboard.getEventKeyState()) {
            y -= 3 * dy;
        } else if(Keyboard.getEventKey() == Keyboard.KEY_A && Keyboard.getEventKeyState()) {
            x -= 3 * dx;
        } else if(Keyboard.getEventKey() == Keyboard.KEY_D && Keyboard.getEventKeyState()) {
            x += 3 * dx;
        }
    }
}

public void drawPlayer() {
    glColor3f(3, 0, 0);

    glPushMatrix();
        glTranslatef(x, y, 0);
        glBegin(GL_QUADS);
            glVertex2f(x - 25, y - 25);
            glVertex2f(x + 25, y - 25);
            glVertex2f(x + 25, y + 25);
            glVertex2f(x - 25, y + 25);
        glEnd();
    glPopMatrix();
}

public long getTime() {
    return (Sys.getTime() * 1000) / Sys.getTimerResolution(); 
}

public double getDelta() {
    long currentTime = getTime();
    double delta = (double)(currentTime - lastFrame);
    lastFrame = getTime();

    return delta;
}

public void update(int dx, int dy) {
    x += dx;
    y += dy;
}

public static void main(String[] argv) {
    new Main();
}
}
1

1 Answers

0
votes

You have too enable repeated key events. Also I would advise you to explicitly create your Keyboard. To do both put this after your Display.create() so it looks like this:

    try {
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.setTitle("Input Demo");
        Display.create();
        Keyboard.create();
        Keyboard.enableRepeatEvents(true);
    } catch (LWJGLException e) {
        //TODO handle exception in a proper way
    }

Also you should stick at one method of checking you input.. The way you check for KEY_W is usually used for polling, though it still works.