SDL input [solved]

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

SDL input [solved]

Post 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;
		}
	}
Last edited by Randi on Fri May 21, 2010 9:42 pm, edited 1 time in total.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: SDL input

Post 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.
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

Re: SDL input

Post by Randi »

thanks for the help, what a silly mistake.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: SDL input

Post by mv2112 »

Randi wrote:thanks for the help, what a silly mistake.
No problem, I've made that mistake before. ;)
Post Reply