Sign in to follow this  
Player_0

Help with using arrow keys in SDL please

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
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

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