Jump to content
  • Advertisement
Sign in to follow this  
Kixdemp

Help with game walking engine, please!

This topic is 4483 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 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! ;-)

Share this post


Link to post
Share on other sites
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

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
Guest Anonymous Poster
oh, and why is that break there, right after the switch??

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