Moving a player while holding down a key.

Started by
20 comments, last by MylesEvans 11 years, 3 months ago
First, don't set all your bools to false at the start of handleKeys(). That defeats the purpose of checking to see if they were released. I'm really surprised you're not getting the opposite problem. The way the code is written there I'd expect your guy to only move on the frame when the key was initially pressed.

Can you confirm that SDL_KEYUP messages are being generated when a key is released?

Well, thats the problem. I don't think SDL_KEYUP is working because when I press the key the player starts moving, but when I let go of it the player is still moving. I need to be able to determine when the key is released so I can stop the player.

Advertisement

So can you test to see if that's what's causing the problem? Just put in a line before your call to handleKeys() that pops a message box or something when an SDL_KEYUP event happens and then start the program and hammer a couple keys to see if the message appears.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
So can you test to see if that's what's causing the problem? Just put in a line before your call to handleKeys() that pops a message box or something when an SDL_KEYUP event happens and then start the program and hammer a couple keys to see if the message appears.

I did something similar. I drew a square in the case after the KEYUP (where it supposed to make the boolean false again).

No square was drawn upon releasing the key so I'm pretty sure that is where the problem lies. Its not registering the KEYUP which is why my character keeps going after I let go of the key.

Are you testing it at the point of event generation (where SDL produces the event) or after some logic has happened?

If you're testing it at the point of event generation then there's either something wrong with your SDL or else something needs to happen in order to allow that event to be generated (I don't use SDL, so I dunno). Otherwise I'd closely check all the logic in between the event generation and the test to make sure that there's no '=' where there should be a '==' and so forth.

You may want to fire up the debugger and try to get to the core of the problem.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

I'm just not figuring this out

Don't test for Key Up (ie Key Release), nor set bools for direction; Rather, set all velocities to 0 before testing for key down, then, when a Key is pressed (let's say Right is pressed), assign the X velocity there.

Like this (pseudoCode):


void Player::Update()
{
  XVelocity = 0;
  YVelocity = 0;

  if (KeyPressed(KEY_UP) {
    YVelocity = -1;
  }
  if (KeyPressed(KEY_DOWN) {
    YVelocity = 1;
  }
  if (KeyPressed(KEY_LEFT) {
    XVelocity = -1;
  }
  if (KeyPressed(KEY_RIGHT) {
    XVelocity = 1;
  }

  // Move the player based on the velocity
  XLocation += XVelocity;
  YLocation += YVelocity;  

  ...

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

The problem is with the key event messages that he's getting from SDL. He doesn't have a 'pressed' function (unless there's one in SDL that he's not using) - he's trying to create one based on key-up/key-down messages.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
The problem is with the key event messages that he's getting from SDL. He doesn't have a 'pressed' function (unless there's one in SDL that he's not using) - he's trying to create one based on key-up/key-down messages.

There is. He should look at SDL_GetKeyState()

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Aye, there we go.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

I tried that method. It was the first thing I tried


If I hold down the key the payer doesn't move. I have to repeatedly tap the key. 

void handleKeys( unsigned char key, int x, int y )
{
Uint8 *keystates = SDL_GetKeyState(NULL);

if( keystates[SDLK_w])
{
PlayerY += 10;
}
else if( keystates[SDLK_s])
{
PlayerY -= 10;
}
else if( keystates[SDLK_d])
{
PlayerX += 10;
}
else if( keystates[SDLK_a])
{
PlayerX -= 10;
}
}

This topic is closed to new replies.

Advertisement