user2972343
user2972343

Reputation: 21

Moving object with SFML

I started studying SFML from the book "SFML Game Development" by Arthur Moreira. I'm trying to move a circle object on the screen using the keyboard. Here is the code that is given:

#include <SFML/Graphics.hpp>

class Game
    {
    public:
        Game();
        void run();
    private:
        void ProcessEvents();
        void update();
        void render();
        void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);

    private:
        sf::RenderWindow myWindow;
        sf::CircleShape myPlayer;
        bool movingLeft, movingUp, movingRight, movingDown;

    };

Game::Game() : myWindow(sf::VideoMode(640, 480), "SFML Game"), myPlayer()
    {
        myPlayer.setRadius(40.f);
        myPlayer.setFillColor(sf::Color::Red);
        myPlayer.setPosition(100.f, 100.f);
    }

void Game::run()
    {
        while (myWindow.isOpen())
        {
            ProcessEvents();
            update();
            render();
        }
    }

void Game::ProcessEvents()
    {
        sf::Event event;
        while (myWindow.pollEvent(event))
            {
                switch(event.type)
                    {
                        case sf::Event::KeyPressed : handlePlayerInput(event.key.code, true); break;
                        case sf::Event::KeyReleased : handlePlayerInput(event.key.code, false); break;
                        case sf::Event::Closed : myWindow.close(); break;
                    }
            }
    }

void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
    {
        if (key == sf::Keyboard::W) movingUp = isPressed;
        else if (key == sf::Keyboard::S) movingDown = isPressed;
        else if (key == sf::Keyboard::A) movingLeft = isPressed;
        else if (key == sf::Keyboard::D) movingRight = isPressed;
    }

void Game::update()
    {
        sf::Vector2f movement(0.f, 0.f);
        if (movingUp) movement.y -= 1.f;
        if (movingDown) movement.y += 1.f;
        if (movingLeft) movement.x -= 1.f;
        if (movingRight) movement.x += 1.f;

        myPlayer.move(movement);
    }

void Game::render()
    {
        myWindow.clear();
        myWindow.draw(myPlayer);
        myWindow.display();
    }

int main()
{
    Game game;
    game.run();

    return 0;
}

Here is what my problem is: In the function update(), I'm updating the direction on which the cicle should go. But the first time when I try to move for example to left, the circle goes to right. The opposite is the same. If I try to go to right in the beginning, the circle goes to left. When I try to go Up and Down the result is the same. So, as I said, when I go to left, it goes to right. But if I press again left, the movement stops and I can move the circle correctlyto left and right. I should do the same thing to repair the direction if I want to go Up and Down. Do I have a mistake in my code?

And my second question is: In the update function I have written the code that is in the book. But if I want to go Up, I think I should add 1 to 'y', not to substract 1. And if I want to go down, shouldn't I substract 1? I think that the update function should look like this:

void Game::update()
    {
        sf::Vector2f movement(0.f, 0.f);
        if (movingUp) movement.y += 1.f; //Here we should add 1
        if (movingDown) movement.y -= 1.f; //Here we should substract 1
        if (movingLeft) movement.x -= 1.f;
        if (movingRight) movement.x += 1.f;

        myPlayer.move(movement);
    }

Maybe it will be better for you to run the code if you are able in order to see what exactly is happening.

Upvotes: 0

Views: 6791

Answers (1)

Collin Dauphinee
Collin Dauphinee

Reputation: 13973

Your moving variables aren't initialized, so some of them may initially have a true value.

You should set them all to false in your Game constructor.

Upvotes: 2

Related Questions