Hey there people
At the moment I'm trying to implement a simple Mario movement, this is what I have at the moment,
Here is my keybaord event checker
if (state == GameStates::GS_Level1)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
//Set move right to true
Characters.at(0).SetMoveRight(true);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
//Set move left to true
Characters.at(0).SetMoveLeft(true);
}
//Releases
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::D)
{
//Set move right to true
Characters.at(0).SetMoveRight(false);
}
else if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::A)
{
//Set move right to true
Characters.at(0).SetMoveLeft(false);
}
}
and here is my character update method
void CharacterObject::UpdateMovement()
{
if (moveRight == true)
{
if (xSpeed < xSpeedMax)
{
xSpeed += xIncrease;
}
}
if (moveLeft == true)
{
if (xSpeed > -(xSpeedMax))
{
xSpeed -= xIncrease;
}
}
if (moveRight == false && moveLeft == false)
{
if (xSpeed > 0)
{
xSpeed -= xIncrease;
}
if (xSpeed < 0)
{
xSpeed += xIncrease;
}
if (xSpeed == 0.2 || xSpeed == -0.2)
{
xSpeed = 0;
}
}
xPos += xSpeed;
yPos += ySpeed;
}
But at the moment sometimes my character will continue to move right, moving left seems fine, but some if I move right and release, my xSpeed will always remain at 0.2 not 0, does anyone have any good tutorial sites on how to implement simple 2d side scroller movement? Or any ideas on how to improve my terrible implementation?