SFML community forums

Help => Graphics => Topic started by: HElwy on July 27, 2013, 01:38:56 am

Title: [SOLVED] Auto Movement
Post by: HElwy on July 27, 2013, 01:38:56 am
First of all, I'm currently reading the SFML Game Development book, in the first chapter, when movement is introduced, I encountered some problems, I spawn a white circle in the centre of the window, now If i press any directional arrow, the circle keeps going in the other direction till I press it, and then it gets back to normal, and horizontal and vertical ones are seperate.

Example: I start "Game", press up, the circle keeps going down, i hold up, the circle stands still, I release, it keeps going down, I press down, it stops, now it responds normally to both keys, it will happen again with the right and left, it gets fixed after the initial error, but I wish to know how I can remove it

#include <SFML/Graphics.hpp>
using namespace sf;

class Game
{
        public:Game();
                void run();

        private:
                void processEvents();
                void update(Time);
                void render();
                void handlePlayerInput(Keyboard::Key,bool);

                bool mIsMovingUp, mIsMovingRight, mIsMovingLeft, mIsMovingDown;
                float playerSpeed;
                Time TimePerFrame;
               

        private:
                RenderWindow mWindow;
                CircleShape mPlayer;
};

Game::Game():mWindow(VideoMode(640, 480), "SFML Application"),mPlayer(), playerSpeed(20.f), TimePerFrame(seconds(1.f / 60.f))
{
        mPlayer.setRadius(20.f);
        mPlayer.setPosition(220.f, 220.f);
        mPlayer.setFillColor(Color::White);
}

void Game::handlePlayerInput(Keyboard::Key key, bool isPressed)
{
        if (key == Keyboard::W || key == Keyboard::Up)
                mIsMovingUp = isPressed;
        else if (key == Keyboard::S || key == Keyboard::Down)
                mIsMovingDown = isPressed;
        else if (key == Keyboard::A || key == Keyboard::Left)
                mIsMovingLeft = isPressed;
        else if (key == Keyboard::D || key == Keyboard::Right)
                mIsMovingRight = isPressed;
}

void Game::run()
{
        Clock clock;
        Time timeSinceLastUpdate = Time::Zero;
        while (mWindow.isOpen())
        {
                processEvents();
                timeSinceLastUpdate += clock.restart();
                while (timeSinceLastUpdate > TimePerFrame)
                {
                        timeSinceLastUpdate -= TimePerFrame;
                        processEvents();
                        update(TimePerFrame);
                }
        render();
        }
}

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

void Game::update(Time deltaTime)
{
        Vector2f movement(0.f, 0.f);
        if (mIsMovingUp)
                movement.y -= playerSpeed;
        if (mIsMovingDown)
                movement.y += playerSpeed;
        if (mIsMovingLeft)
                movement.x -= playerSpeed;
        if (mIsMovingRight)
                movement.x += playerSpeed;

        mPlayer.move(movement * deltaTime.asSeconds());
}

void Game::render()
{
        mWindow.clear();
        mWindow.draw(mPlayer);
        mWindow.display();
}

int main()
{
        Game game;
        game.run();
}
Title: Re: Auto Movement
Post by: eXpl0it3r on July 27, 2013, 01:51:23 am
There seems to be something off with your event queue from the OS. What OS are you using and if you're on Linux, what window manager are you using?

Everything works fine for me. :-\
Title: Re: Auto Movement
Post by: The Hatchet on July 27, 2013, 02:35:37 am
Worked fine for me too.

WinXP Code::Blocks 12.11
Title: Re: Auto Movement
Post by: HElwy on July 27, 2013, 11:04:31 am
So I have to downgrade my OS or use linux?
Title: Re: Auto Movement
Post by: zsbzsb on July 27, 2013, 01:28:28 pm
So I have to downgrade my OS or use linux?

The problem is not your OS, everything SFML works fine for me on Windows 8. If I were you I would step through the code you are using and see where it goes wrong.
Title: Re: [SOLVED] Auto Movement
Post by: HElwy on July 27, 2013, 09:15:31 pm
Just had to initialize all the booleans to false