Currently my object is moving constantly when i hold down whichever arrow key. How do I make it move a set distance each time it is pressed?
Thanks
// with instance boolean field
private boolean directionKeyDown; // initial default value is 'false'
// in act or method it calls
boolean leftDown = Greenfoot.isKeyDown("left");
boolean rightDown = Greenfoot.isKeyDown("right");
boolean upDown = Greenfoot.isKeyDown("up");
boolean downDown = Greenfoot.isKeyDown("down");
if (directionKeyDown != (leftDown || rightDown || upDown || downDown) &&
(directionKeyDown = !directionKeyDown))
{
int dx = 0, dy = 0;
if (rightDown) dx++;
if (leftDown) dx--;
if (upDown) dy--;
if (downDown) dy++;
setLocation(getX()+dx, getY()+dy);
}