Sign in to follow this  

Multiple keypresses with SDL

This topic is 4356 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Up until now, my tilesbased RPG has only had four directions of movement. I wanted to add so that you could walk diagonally by pressing two arrow keys at the same time. The code seems to be working, the only potential error I could find were where I take the actual input. Is it able to check two keystates at once with SDL? Heres what it looks like:
Uint8 *keystate = SDL_GetKeyState(NULL);	// check the keyboard

			// 7 0 1
			// 6   2
			// 5 4 3
		if(!keystate[SDLK_UP] && !keystate[SDLK_RIGHT] && !keystate[SDLK_DOWN] && !keystate[SDLK_LEFT]) {
			Wizard.SwitchAnimation("idle");
			Wizard.MoveFlag(false);	// standing still
		}
		else {
			Wizard.SwitchAnimation("walking");
			Wizard.MoveFlag(true);
			
			if(keystate[SDLK_UP]) 
				Wizard.SetDirection(0);
			else if(keystate[SDLK_RIGHT] && keystate[SDLK_UP]) 
				Wizard.SetDirection(1);
			else if(keystate[SDLK_RIGHT])	
				Wizard.SetDirection(2);
			else if(keystate[SDLK_RIGHT] && keystate[SDLK_DOWN]) 
				Wizard.SetDirection(3);
			else if(keystate[SDLK_DOWN]) 
				Wizard.SetDirection(4);
			else if(keystate[SDLK_DOWN] && keystate[SDLK_LEFT]) 
				Wizard.SetDirection(5);
			else if(keystate[SDLK_LEFT]) 
				Wizard.SetDirection(6);
			else if(keystate[SDLK_LEFT] && keystate[SDLK_UP]) 
				Wizard.SetDirection(7);
		}

Up, down, left and right works ok. But not the diagonal stuff. I placed a break point on the "SetDirection(1)" just below when I test if UP and RIGHT were pressed at the same time. I started the program and pressed those keys but nothing happened, breakpoint never reached. Instead I switch the statements so that pressing just RIGHT had the same effect as pressing UP and RIGHT at the same time. And my little wizard character started to move diagonally, so I dare to say the rest of the code is working. How should I take multiple key presses?

Share this post


Link to post
Share on other sites
what you do:

if up is on then Wizard.SetDirection(0) but if not then check if right and up are one if so Wizard.SetDirection(1) ...

see ...

what you want would be something like this:



if( up )
{
if( right ) // check if right is pressed as well
{
...
}
else if( left) // let's see if left is pressed as well
{
....
}
else // left and right are NOT pressed
{
....
}
}
else if ( down ) // here we know that up is not pressed!
{
if( right ) // check if right is pressed as well
{
...
}
else if( left) // let's see if left is pressed as well
{
....
}
else // left and right are NOT pressed
{
....
}
}
else if( right ) // here we know that up and down are not pressed!
{

}
else if( left ) // only left is left :D
{
....
}




or you could simply remove your else statements like so:



if( up )
set direction for up
if( up and right)
set direction for up and right // this will override the above call to set direction
...


Share this post


Link to post
Share on other sites

This topic is 4356 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this