Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to implement 2D side scroller  (Read 1236 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
How to implement 2D side scroller
« on: June 23, 2014, 09:24:50 pm »
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?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: How to implement 2D side scroller
« Reply #1 on: June 23, 2014, 09:33:56 pm »
You are mixing realtime input "sf::Keyboard::isKeyPressed" with events "event.type == sf::Event::KeyReleased". That's usually a bad idea. Using one or the other is usually a better approach.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to implement 2D side scroller
« Reply #2 on: June 29, 2014, 05:03:00 pm »
I'm not sure what "0.2" is exactly but I'm guessing that it's the value of xIncrease (I would probably name this xDelta as when going left, the value decreasing, not increasing). It looks like you are testing to see if the speed has passed zero by the amount in xIncrease and then resetting the speed if it has. Unfortunately, floating point numbers are not accurate enough to be testing accurately so you should instead be testing to see if the speed value is within a range each time. For example, you could try:
if (moveRight == false && moveLeft == false)
{
    if (xSpeed > xIncrease)
        xSpeed -= xIncrease;
    else if (xSpeed < xIncrease)
        xSpeed += xIncrease;
    else
        xSpeed = 0;
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything