Help with game walking engine, please!

Started by
6 comments, last by Kixdemp 18 years, 1 month ago
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: // 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; } The guy only walks 1 spot, even when I have the arrow pressed... What can I do? Thanks! ;-)
Advertisement
// 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
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
Nope - now he doesn't move at all... :'-(

Any more suggestions? Thanks! ;-)
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.
oh, and why is that break there, right after the switch??
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
Use SDL_GetKeyState. Check the SDL documentation, it handles key-repeat.
It works! Thanks very much! :-D

This topic is closed to new replies.

Advertisement