Pressing vs. holding down a button

In my game, I am trying to make the character switch sprites after a certain time. When I simply press and release a button, it switches sprites no problem. However, when I try to hold the button, it take a second or so to start changing the sprite.

Try it out here -

http://www.mediafire.com/?5vjgsqo30y5dmo0

Sprite switching code -

public void keyPressed(KeyEvent e)
	{
		int key = e.getKeyCode();
		
		if (key == KeyEvent.VK_A)
		{
			if ((key2 != 2) && (key2 != 3) && (key2 != 4))
			{
				if (imgSwitchHoriz) player = leftRunning.getImage();
				if (!imgSwitchHoriz) player = leftStill.getImage();
				key2 = 1;
				dx = negChange;
				if (imgSwitchHoriz) imgSwitchHoriz = false;
				else if (!imgSwitchHoriz) imgSwitchHoriz = true;
			}
		}
		
		if (key == KeyEvent.VK_D) 
		{
			if ((key2 != 1) && (key2 != 3) && (key2 != 4))
			{
				if (imgSwitchHoriz) player = rightRunning.getImage();
				if (!imgSwitchHoriz) player = rightStill.getImage();
				key2 = 2;
				dx = posChange;
				if (imgSwitchHoriz) imgSwitchHoriz = false;
				else if (!imgSwitchHoriz) imgSwitchHoriz = true;
			}
		}	
		
		if (key == KeyEvent.VK_W)
		{
			if ((key2 != 1) && (key2 != 2) && (key2 != 4))
			{
				if (imgSwitchVert) player = up.getImage();
				if (!imgSwitchVert) player = up2.getImage();
				key2 = 3;
				dy = negChange;
				if (imgSwitchVert) imgSwitchVert = false;
				else if (!imgSwitchVert) imgSwitchVert = true;
			}
		}
		
		if (key == KeyEvent.VK_S) 
		{
			if ((key2 != 1) && (key2 != 2) && (key2 != 3))
			{
				if (imgSwitchVert) player = down.getImage();
				if (!imgSwitchVert) player = down2.getImage();
				key2 = 4;
				dy = posChange;
				if (imgSwitchVert) imgSwitchVert = false;
				else if (!imgSwitchVert) imgSwitchVert = true;
			}
		}
		if (key == KeyEvent.VK_ESCAPE) main.menuInit();
	}

That’s a normal keyboard feature where there is an input lag between the first key event and all subsequent key event.

Poop.

Is there a way around this? I guess my only example is Minicraft (https://s3.amazonaws.com/ld48/ld22/index.html) where they transition flawlessly.

p.s. yes I stole my sprite from there

In the keyPressed section set a boolean variable that represents your key to true. and do the opposite in the keyReleased section. Then in your main loop check to see if the key is true or false and do accordingly.

For example:


//In your variable declaration section
boolean keyA = false;
///etc...

//In your keyPressed event
if (key == KeyEvent.VK_A)
      {
         keyA = true;
      }

//In your keyReleased event
if (key == KeyEvent.VK_A)
{
    keyA = false;
}

//Somewhere in your main loop
if (keyA)
{
    if ((key2 != 2) && (key2 != 3) && (key2 != 4))
         {
            if (imgSwitchHoriz) player = leftRunning.getImage();
            if (!imgSwitchHoriz) player = leftStill.getImage();
            key2 = 1;
            dx = negChange;
            if (imgSwitchHoriz) imgSwitchHoriz = false;
            else if (!imgSwitchHoriz) imgSwitchHoriz = true;
         }
}

The only way around this is to have a boolean variable that you set to true if pressed and false if released. Then in your game loop you would apply the appropriate actions depending on the state of the variables.

EDIT: Aggh gbeebe beat me to it :S

…and I modified it, adding code to help what we both are trying to say :slight_smile: