Jump to content
  • Advertisement
Sign in to follow this  
Player_0

Help with using arrow keys in SDL please

This topic is 4434 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

Hello! Heres some code...
int KeyInfo(SDL_Event hi)
{
	int num = -5;

	if(hi.type == SDL_KEYDOWN){
		switch(hi.key.keysym.sym){
			case SDLK_UP:
				yvel=1;
				num=-1;
				break;
			case SDLK_DOWN:
				yvel=-1;
				num=-1;
				break;
			case SDLK_RIGHT:
				xvel=1;
				num=1;
				break;
			case SDLK_LEFT:
				xvel=-1;
				num=1;
				break;
			default:
				printf("hi");
				break;
		}
	}//	if(hi.type == SDL_KEYDOWN){

	if(hi.type == SDL_KEYUP){
		if(num)	{xvel = 0; printf("num=1, UP");}
		else if(!num)	{yvel = 0; printf("num=-1, DOWN");}
	}//	if(hi.type == SDL_KEYUP){

	return 1;
}

Thats the function. The xvel works but the yvel doesnt. When i press up and down "num=1, UP" is printed, the same as the left and right key. Its not recognizing when !num is true so its never setting yvel to 0. Is there something Im doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Why not:


int KeyInfo(SDL_Event hi)
{
if(hi.type == SDL_KEYDOWN){
switch(hi.key.keysym.sym){
case SDLK_UP:
yvel=1;
break;
case SDLK_DOWN:
yvel=-1;
break;
case SDLK_RIGHT:
xvel=1;
break;
case SDLK_LEFT:
xvel=-1;
break;
default:
printf("hi");
break;
}
}
else if(hi.type == SDL_KEYUP)
{
switch(hi.key.keysym.sym){
case SDLK_UP:
yvel=0;
break;
case SDLK_DOWN:
yvel=0;
break;
case SDLK_RIGHT:
xvel=0;
break;
case SDLK_LEFT:
xvel=0;
break;
default:
printf("hi");
break;
}
}// if(hi.type == SDL_KEYUP){

return 1;
}




AS for why it wasn't working before, i(!value) is like if( value != 0 ), not if( value > 0 ) which is what your code assumed.

Share this post


Link to post
Share on other sites
I changed it to
if(num==-1)
But that didnt work.


Quote:

AS for why it wasn't working before, i(!value) is like if( value != 0 ), not if( value > 0 ) which is what your code assumed.


I thought (num) meant true or above 0 while (!num) meant false or lower than 0 not just !=0.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!