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
Randi
Chaos Rift Cool Newbie
Posts: 50 Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville
Post
by Randi » Fri May 21, 2010 9:32 pm
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.
mv2112
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:
Post
by mv2112 » Fri May 21, 2010 9:38 pm
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:
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
Posts: 50 Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville
Post
by Randi » Fri May 21, 2010 9:42 pm
thanks for the help, what a silly mistake.
mv2112
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:
Post
by mv2112 » Fri May 21, 2010 9:47 pm
Randi wrote: thanks for the help, what a silly mistake.
No problem, I've made that mistake before.