Diagonal keyboard movement in java

Ok I have a square. I want to use the 4 directional keyboard buttons to move it around by changing its x and y cooardinates. I can move it anyplace except
for diagonally. This is because only the last pressed and held down key is firing the event and keyTyped( ). Therefore, to move diagonally, you have to
do something like this: UP, RIGHT, UP,RIGHT…etc rather than keeping the buttons pressed down simultaneously which will result in a non-diagonal movement
according to the last pressed button. If anyone can help me by first explaining
the concept of what I should do, then if necessary show me some code I would appreciate it.

Another thing is, when I want to move into a certain direction, let’s say to the right, I keep pressing right. However, the first movement is a movement
followed by a small delay, then the movement continues normally. If you want to know what I mean just keep holding down a letter button now and see how the
first typed letter and the rest of the letters are not typed in the same speed. How do I get rid of this to make the movement more appropriate? Concept is always
more important so please don’t just paste some code and tell me to use it, thank you.

Use flags for the key states. Eg if a button is pressed you set the variable for that button to true and if its released you set it to false.

In your loop you simply check the state of the variable and react accordingly. Like

//get the deltas
if right dx+=5
if left dx-=5
if down dx+=5
if up dx-=5
//update player position
x+=dx
y+=dy

Note that I didn’t use else… this way pressing left and right together results in no horizontal movement (+5-5=0).

Edit: Oh and using deltas instead of modfying the position directly allows things like friction and acceleration to be added easily.

Yes, DONT USE ELSE! I am using something like

    if (Keyboard.isKeyDown(Keyboard.KEY_W) )) {
       camera.move(camera.getMoveParam() * time * 10);
    }

    if (Keyboard.isKeyDown(Keyboard.KEY_A) )) {
        camera.strafe(-camera.getMoveParam() * time / 2);
    }


and it is working fine. When program is running, and you have pressed W + A, you first move camera forward and then to the left…

further more, you set your flags with the following methods:

private boolean[] keys=new boolean[256];
public void KeyPressed(KeyEvent e)
{
keys[e.getKeyCode()]=true;
}
public void keyReleased(KeyEvent e)
{
keys[e.getKeyCode()]=false;
}

// in your move method…
if(keys[KeyEvent.VK_UP])//Up is pressed
{
charecter.moveUp();
}
if(keys[KeyEvent.VK_RIGHT])//Up is pressed
{
charecter.moveRIGHT();
}

also, it’ll be good practice to implement a focus listener and set all the keys to false if you lose focus, as opposed to those games where if you minimize the window and then return to it, a key can get “stuck” and your charecter keeps on shooting though you’re not pressing any key.
hope this helps,
noam

keys[e.getKeyCode()&0xFF]=true;

Just a side note. You can avoid all these AWT type behaviors by using JInput.

Umm, you are getting some pretty random helping here. Some people are giving you LWJGL help, for example. And I think using flags, while certainly a way to do it, is unnecessary and non-intuitive.

With regular AWT you can only keep track of one key at a time. However, there is a simple solution: make a LinkedList and add keys as they are pressed, remove them as they are released. KeyTyped is not needed. You can see this is the same general concept as the flag solution, but much less memory is allocated, you can handle every possible key press, and it is completely intuitive. This is how I implement all my AWT games.


LinkedList buttonsPressed = new LinkedList();

public void keyPressed(KeyEvent e)
{
    buttonsPressed.add(e);
}

public void keyReleased(KeyEvent e)
{
    if (buttonsPressed.contains(e))
        buttonsPressed.remove(e);
}

private void processKeys()
{
    for (Iterator i = buttonsPressed.iterator(); i.hasNext();)
    {
        int code = ((KeyEvent)i.next()).getKeyCode();
        if (code == KeyEvent.VK_UP)
            moveUp();
        else if (code == KeyEvent.VK_DOWN)
            moveDown();
        //etc. etc.
    }
}

And voila. Does that make sense to you?