2D Top Down Shooter Movement

-Sorry for the lengthty post
Hi I got source code from the Articles & tutorials section from the first post “Basic Game.” I implemented WASD control over a square that is being drawn on the screen but I’m having a small issue. If I’m moving right and click “A” without letting go of “D” I will eventually stop moving once I let go of “D” and then continue moving left again because I’m still holding down “A”.

private double x = 0;
private double y = 0;
private int velX, velY = 0;
private int movementSpeed = 4;

protected void update(int deltaTime) {
	if (y > 700 - 30) { //Make sure square doesn't leave the screen
		y = 700 - 30;
	} else if (y < 0) {
		y = 0;
	} else {
		y += velY;
	}
  if (x > 1000 - 30) { //Make sure square doesn't leave the screen
		x = 1000 - 30;
	} else if (x < 0) {
		x = 0;
	} else {
		x += velX;
	}
    }

protected void render(Graphics2D g) {
            g.fillRect((int) x, (int) y, 30, 30);
    }

@Override
public void keyPressed(KeyEvent e) {
	if (e.getKeyCode() == KeyEvent.VK_W) {
		velY = -movementSpeed;
	}
	if (e.getKeyCode() == KeyEvent.VK_A) {
		velX = -movementSpeed;
	}
	if (e.getKeyCode() == KeyEvent.VK_S) {
		velY = movementSpeed;
	}
	if (e.getKeyCode() == KeyEvent.VK_D) {
		velX = movementSpeed;
	}
     }

@Override
public void keyReleased(KeyEvent e) {

	if (e.getKeyCode() == KeyEvent.VK_W) { 
		velY = 0;
	}
	if (e.getKeyCode() == KeyEvent.VK_A) {
		velX = 0;
	}
	if (e.getKeyCode() == KeyEvent.VK_S) {
		velY = 0;
	}
	if (e.getKeyCode() == KeyEvent.VK_D) {
		velX = 0;
	}
}

Thank you SO much for any help you can offer.

So what exactly is the problem? This is normal and expected behavior from the code. :slight_smile:

I would make a boolean array of keys down, eg.

private boolean keydown[255]; //make this a field

public void keyPressed(KeyEvent e) {
    keydown[e.getKeyCode()] = true;
}

public void keyReleased(KeyEvent e) {
    keydown[e.getKeyCode()] = false;
}

then in the update method go

if (keydown[KeyEvent.VK_W])
{
    velY = -movementSpeed; 
}
//do the same for the rest

What this does is it stores all keypresses, you are no longer hindered by only changing velocity when the keydown or key up events are trigged. You ALWAYS know what key is down and which is not :slight_smile:

you can then go

if (keydown[KeyEvent.VK_A] && keydown[KeyEvent.VK_D]) //both keys are down
{
    //don't change the velocity because both keys are down
    //or you could set velX to 0, it depends how you want the game to work.
}
else if (keydown[KeyEvent.VK_A])
{
    velX = -movementSpeed; 
} else if (keydown[KeyEvent.VK_D])
{
    velX = movementSpeed; 
}

I’m wondering if anyone knows a way to implement WASD movement with you being able to move smoothly, so no un-wanted stops.

Ah, then either go for roland’s idea, or just simply 4 booleans: upPressed, downPressed, leftPressed, and rightPressed; which you would set to true/false based on whether they’re pressed or not :slight_smile:

also: don’t just go VelX = movementSpeed
GO velX += movementSpeed or velX -= movementSpeed

this will be smooth as long as movementSpeed isnt too big.
Otherwise you can instantly switch between positive and negative velocity (bad)

movementSpeed will probably have to be about ~0.1 for this

Delayed response because I was trying to get it to work, and now it works as intended!

roland and ra4king thank you so much!!! : D