Sign in to follow this  
flodywan

SDL Key Event Doing Opposite

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

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