Jump to content
  • Advertisement
Sign in to follow this  
flodywan

SDL Key Event Doing Opposite

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

I have a pong game that's supposed to have the left paddle move up/down with the w-key and s-key. But, at the very start of the program it does the opposite (ex. s moves it up, w moves it down) then after a second it will "fix" itself.
Any ideas how to fix this?



void Game::Event_Handler(SDL_Event * Event)
{

puck_offset.x += xvel;
puck_offset.y += yvel;
Collision();

if(Event->type == SDL_KEYDOWN)
keyboardBuffer[Event->key.keysym.sym] = true;
else if(Event->type == SDL_KEYUP)
keyboardBuffer[Event->key.keysym.sym] = false;

if (keyboardBuffer[SDLK_w])
{
pad1_offset.y -= 3;
if (pad1_offset.y < 0)
pad1_offset.y += 3;
}

if (keyboardBuffer[SDLK_s])
{
pad1_offset.y += 3;
if (pad1_offset.y + pad1_offset.h > 480)
pad1_offset.y -= 3;
}

if (keyboardBuffer[SDLK_UP])
{
pad2_offset.y -= 3;
if (pad2_offset.y < 0)
pad2_offset.y += 3;
}
if (keyboardBuffer[SDLK_DOWN])
{
pad2_offset.y += 3;
if (pad2_offset.y + pad2_offset.h > 480)
pad2_offset.y -= 3;
}

}


Share this post


Link to post
Share on other sites
Advertisement
pad1_offset is an SDL_Rect for the left paddle, which I use later to update the left paddle with SDL_BlitSurface(pad1, NULL, screen, pad1_offset);

I'm confused because it's the same exact thing as pad2, except pad2 uses the up and down keys.

And yes, I initialize pad1 as I initialize pad2:


pad1_offset.y = 200;
pad1_offset.x = 0;
pad1_offset.h = paddle1->clip_rect.h;
pad1_offset.w = paddle1->clip_rect.w;
pad2_offset.y = 200;
pad2_offset.h = paddle2->clip_rect.h;
pad2_offset.x = 625;
puck_offset.x = 312;
puck_offset.w = puck->clip_rect.w;
puck_offset.y = 200;
puck_offset.h = puck->clip_rect.h;

Share this post


Link to post
Share on other sites
Well, I assume

if (pad1_offset.y < 0)
pad1_offset.y += 3;


as the culprit, that's why I asked about type and initialisation. That snippet is supposed to keep your paddle inside screen bounds.

Simplest way would be to show the values of pad1_offset.y via debugger. Step into it and look at its values. Or display them on screen somewhere. Things should get obvious from there.

Share this post


Link to post
Share on other sites
Hmm I've looked through the debugger and the value seems to be updated fine. I still can't figure this one out. It's weird, it only is messed up at the beginning. I'll press 'w', it will go down. I release 'w', and then press 'w' again and it goes up, and from then on 's' and 'w' work perfectly.

Share this post


Link to post
Share on other sites
I just found something interesting. I tried using a different image for my puck (actually an array of images to be animated), and the same thing started happening to the right paddle! I changed it back, and the right paddle is now fixed. So, could this be a memory issue or something?

Share this post


Link to post
Share on other sites
FIXED IT!! My keyboardBuffer(kept track of key states) wasn't getting initialized properly. Thanks so much for pointing me to the initialization direction.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!