Page 1 of 1

SDL input [solved]

Posted: Fri May 21, 2010 9:32 pm
by Randi
I don't get what I'm doing wrong, I am attempting to make is so when I press the b key it only does the action once, right now it loops through a whole bunch of times.

Code: Select all

bool battle = false;
bkey = false

while(!done)
{
	//key input
	Uint8 *key = SDL_GetKeyState (NULL);
	if (key[SDLK_b])
	{
		if (bkey == false)
		{
			bkey = true;
			if(battle == true)
			{
			battle = false;
			}
			else if(battle == false)
			{
			battle = true;
			}
		}
		else
		{
		bkey = false;
		}
	}

Re: SDL input

Posted: Fri May 21, 2010 9:38 pm
by mv2112
Randi wrote:I don't get what I'm doing wrong, I am attempting to make is so when I press the b key it only does the action once, right now it loops through a whole bunch of times.

Code: Select all

bool battle = false;
bkey = false

while(!done)
{
	//key input
	Uint8 *key = SDL_GetKeyState (NULL);
	if (key[SDLK_b])
	{
		if (bkey == false)
		{
			bkey = true;
			if(battle == true)
			{
			battle = false;
			}
			else if(battle == false)
			{
			battle = true;
			}
}
		else
		{
		bkey = false;
		}
	}
Try putting the:

Code: Select all

else
{
bkey = false;
}
Here:

Code: Select all

if(key[SDLK_b])
{
    //do whatever you posted
}
else
{
   bkey=false;
}
The one you wrote gets the b input, sets the bool to true and does whatever, BUT, if its still pressed, it resets it to false. Your else statement is in the wrong place.

Re: SDL input

Posted: Fri May 21, 2010 9:42 pm
by Randi
thanks for the help, what a silly mistake.

Re: SDL input

Posted: Fri May 21, 2010 9:47 pm
by mv2112
Randi wrote:thanks for the help, what a silly mistake.
No problem, I've made that mistake before. ;)