Sign in to follow this  
Kixdemp

Help with game walking engine, please!

Recommended Posts

Hello everyone! :-D Been a long time since I don't post here... :-P Alright, I'm following the instructions on this site: http://gpwiki.org/index.php/SDL:Tutorials:Practical_Keyboard_Input It doesn't seem to work... Here's my code: [CODE] // Poll for events, and handle the ones we care about. SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_UP: //yPos -= CSIZE; yVel = -CSIZE; break; case SDLK_DOWN: //yPos += CSIZE; yVel = CSIZE; break; case SDLK_LEFT: //xPos -= CSIZE; xVel = -CSIZE; break; case SDLK_RIGHT: //xPos += CSIZE; xVel = CSIZE; break; } break; case SDL_KEYUP: switch (event.key.keysym.sym) { case (SDLK_ESCAPE): case SDL_QUIT: return 0; break; case (SDLK_UP): yVel = 0; break; case (SDLK_DOWN): yVel = 0; break; case (SDLK_LEFT): xVel = 0; break; case (SDLK_RIGHT): xVel = 0; break; } break; } int temp_x = xPos + xVel; int temp_y = yPos + yVel; // Make sure the character didn't go off screen bounds if ((temp_x > -1) && (temp_x < WIDTH)) xPos += xVel; if ((temp_y > -1) && (temp_y < HEIGHT)) yPos += yVel; } [/CODE] The guy only walks 1 spot, even when I have the arrow pressed... What can I do? Thanks! ;-)

Share this post


Link to post
Share on other sites

// Poll for events, and handle the ones we care about.
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
break;
case SDLK_UP:
//yPos -= CSIZE;
yVel = -CSIZE;
break;
case SDLK_DOWN:
//yPos += CSIZE;
yVel = CSIZE;
break;
case SDLK_LEFT:
//xPos -= CSIZE;
xVel = -CSIZE;
break;
case SDLK_RIGHT:
//xPos += CSIZE;
xVel = CSIZE;
break;
}

switch (event.key.keysym.sym)
{
case (SDLK_ESCAPE):
case SDL_QUIT:
return 0;
break;
case (SDLK_UP):
yVel = 0;
break;
case (SDLK_DOWN):
yVel = 0;
break;
case (SDLK_LEFT):
xVel = 0;
break;
case (SDLK_RIGHT):
xVel = 0;
break;
}
break;
}

int temp_x = xPos + xVel;
int temp_y = yPos + yVel;

// Make sure the character didn't go off screen bounds
if ((temp_x > -1) && (temp_x < WIDTH)) xPos += xVel;
if ((temp_y > -1) && (temp_y < HEIGHT)) yPos += yVel;
}






So I can read it...now let me look at it and maybe I can get back to you.


I would change the while loop to be like while(true){...} and then before the first switch, add the line SDL_PollEvent(&event); Like this:



// Poll for events, and handle the ones we care about.
SDL_Event event;
while (true)
{
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
break;
case SDLK_UP:
//yPos -= CSIZE;
yVel = -CSIZE;
break;
case SDLK_DOWN:
//yPos += CSIZE;
yVel = CSIZE;
break;
case SDLK_LEFT:
//xPos -= CSIZE;
xVel = -CSIZE;
break;
case SDLK_RIGHT:
//xPos += CSIZE;
xVel = CSIZE;
break;
}

switch (event.key.keysym.sym)
{
case (SDLK_ESCAPE):
case SDL_QUIT:
return 0;
break;
case (SDLK_UP):
yVel = 0;
break;
case (SDLK_DOWN):
yVel = 0;
break;
case (SDLK_LEFT):
xVel = 0;
break;
case (SDLK_RIGHT):
xVel = 0;
break;
}
break;
}

int temp_x = xPos + xVel;
int temp_y = yPos + yVel;

// Make sure the character didn't go off screen bounds
if ((temp_x > -1) && (temp_x < WIDTH)) xPos += xVel;
if ((temp_y > -1) && (temp_y < HEIGHT)) yPos += yVel;
}




Hope that helps.


-AJ

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you don't seem to have the key state swiches seperated, and as a result your cases aere both setting the velocity values, then on break instead leaving of the switch you are testing for the event type again and clearing it.

bracket up the event type switch's and add an SDLkeyup swithc, so it does one or the other...I'm in an internet cafe so its tricky for me to show you.

Share this post


Link to post
Share on other sites
Hmm... I don't get it... :-S

You're saying that I have to remove the break from the "case SDL_KEYUP:" line? If I do that, the dude doesn't move... :-S

And as for the brackets, did you mean this?:


case SDL_KEYDOWN:
{
<switch goes here>
}

case SDL_KEYUP:
{
<switch goes here>
}


Or something else? Thanks! ;-)


PS: How do you use code tags in this forum? :-P

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