lwjgl isKeyDown canceling out other keys

Posted by AKrush95 on Stack Overflow See other posts from Stack Overflow or by AKrush95
Published on 2012-07-09T20:56:36Z Indexed on 2012/07/09 21:15 UTC
Read the original article Hit count: 166

Filed under:
|
|
|

While trying to create a simple game where a square is manipulated via the keyboard keys, I have come across a small, rather irritating problem. I would like it to work so that when the opposite directional key is pressed, the character will stop; the character may move the other two directions while stopped in this situation.

This works perfectly with LEFT and RIGHT held down; the player may move UP or DOWN. If UP and DOWN are held down, however, the player will not move, nor will Java recognize that the LEFT or RIGHT keys were pressed.

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

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

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

public class Main {
    private Man p;
    private ArrayList<Integer> keysDown, keysUp;
    public Main() {
        try {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.setTitle("LWJGLHelloWorld");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }

        p = new Man(0, 0);
        keysDown = new ArrayList<>();
        keysUp = new ArrayList<>();

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 640, 480, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);

        while (!Display.isCloseRequested()) {
            glClear(GL_COLOR_BUFFER_BIT);

            checkKeys();

            p.draw();

            Display.update();
            Display.sync(60);
        }

        Display.destroy();
    }

    public void checkKeys() {
        ArrayList<Integer> keys = new ArrayList<>();
        keys.add(Keyboard.KEY_A);
        keys.add(Keyboard.KEY_D);
        keys.add(Keyboard.KEY_W);
        keys.add(Keyboard.KEY_S);

        for (int key : keys) {
            if (Keyboard.isKeyDown(key)) 
                keysDown.add(key);
            else 
                keysUp.add(key);
        }

        keysDown.removeAll(keysUp);
        keysUp = new ArrayList<>();

        int speed = 4;
        int dx = 0;
        int dy = 0;
        if (keysDown.contains(keys.get(2))) {
            System.out.println("keyUP");
            dy -= speed;
        }
        if (keysDown.contains(keys.get(3))) {
            System.out.println("keyDOWN");
            dy += speed;
        }
        if (keysDown.contains(keys.get(0))) {
            System.out.println("keyLEFT");
            dx -= speed;
        }
        if (keysDown.contains(keys.get(1))) {
            System.out.println("keyRIGHT");
            dx += speed;
        }
        //if (keysDown.contains(keys.get(0)) && keysDown.contains(keys.get(1))) dx = 0;

        //if (keysDown.contains(keys.get(2)) && keysDown.contains(keys.get(3))) dy = 0;
        p.update(dx, dy);

    }

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

    class Man {
        public int x, y, w, h;
        public float cR, cG, cB;

        public Man(int x, int y) {
            this.x = x;
            this.y = y;
            w = 50;
            h = 50;

            Random rand = new Random();
            cR = rand.nextFloat();
            cG = rand.nextFloat();
            cB = rand.nextFloat();
        }

        public void draw() {
            glColor3f(cR, cG, cB);
            glRecti(x,  y,  x+w,  y+h);
        }

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

That is the code that I am working with. In addition, I am unsure how to compile an executable jar that is using the lwjgl library in addition to slick-util.

© Stack Overflow or respective owner

Related posts about java

Related posts about opengl