Yes, as Kercyn says, your keyUpEvent and keyDownEvent are indeed swapped.
Also, just as an example, I'll post a different way you could do it.
void Player::update(int elapsed_time_ms)
{
sprite->update(elapsed_time_ms);
//OK, this 2 variables don't exist in your code (user_pressed_left and user_pressed_right), but they are
//simply bools that are set, if the left or right keys are pressed.
//EDIT: Here, there's no 'else' in the second 'if', because if the user is pressing both directional keys,
// the acceleration gained from both will cancel each other, and will behave as if no key is pressed.
//Update acceleration based on user input
if(user_pressed_left)
acceleration_x_ -= kWalkingAcceleration * elapsed_time_ms;
if(user_pressed_right)
acceleration_x_ += kWalkingAcceleration * elapsed_time_ms;
//Make sure that the acceleration doesn't exceed the maximum speed
if(acceleration_x_ > kMaxSpeedX)
acceleration_x_ = kMaxSpeedX;
//Same goes for negative acceleration (moving left)
//EDIT: Added 'else' here, because acceleration can only be either max positive or max negative
else if(acceleration_x_ < -kMaxSpeedX)
acceleration_x_ = -kMaxSpeedX;
//Update player's position
x_ += acceleration_x_;
//Now, you can apply kSlowdownFactor (AKA friction).
//The simplest way, is to check if the player has not pressed any direction buttons (or both),
//and apply friction if so.
if((!user_pressed_left && !user_pressed_right) || //No keys pressed
( user_pressed_left && user_pressed_left ) ) //Both keys pressed
{
//EDIT: If the acceleration is 0.0f, then the player is already stopped, so don't apply friction
// By the way, be careful with equal comparison of floating point numbers.
if(acceleration_x_ == 0.0f)
break;
//If acceleration is negative, apply friction to get it closer to 0.0f
if(acceleration_x_ < 0.0f)
{
//EDIT: If acceleration is bigger than friction, then just apply it
if(acceleration_x_ < -kSlowdownFactor)
acceleration_x_ += kSlowdownFactor;
//EDIT: But if acceleration would become positive, then just set it to 0.0
else
acceleration = 0.0f;
}
//Same goes for positive acceleration
//EDIT: Also added 'else' here.
else if(acceleration_x_ > 0.0f)
{
//EDIT: If acceleration is bigger than friction, then just apply it
if(acceleration_x_ > kSlowdownFactor)
acceleration_x_ -= kSlowdownFactor;
//EDIT: But if acceleration would become negative, then just set it to 0.0
else
acceleration = 0.0f;
? }
}
//Keep player within screen bounds
if(x_ < 0)
x_ = 0;
else if(x_ > 640)
x_ = 600;
}
void Player::startMovingLeft()
{
user_pressed_left = true;
}
void Player::startMovingRight()
{
user_pressed_right = true;
}
void Player::stopMoving()
{
//Do nothing
return;
}
Note that you don't need the velocity variable this way
This code is untested, so sorry if there're any errors.
I hope the explanation is concise, but if it's not (or you have any doubt) hit me up, and I'll try to explain better,
Hope it helps.
EDIT: I forgot one thing, and that is to stop applying friction when the acceleration would become 0.0. The way it is now, the player would "wiggle" in place, due to the fact that acceleration keeps jumping from positive to negative.
Also added a few "else" to make the code a bit more efficient.
Sorry for the mistake.