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

Author Topic: [SOLVED] Auto Movement  (Read 3355 times)

0 Members and 1 Guest are viewing this topic.

HElwy

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
[SOLVED] Auto Movement
« 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();
}
« Last Edit: July 27, 2013, 09:14:23 pm by HElwy »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Auto Movement
« Reply #1 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. :-\
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Auto Movement
« Reply #2 on: July 27, 2013, 02:35:37 am »
Worked fine for me too.

WinXP Code::Blocks 12.11

HElwy

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Auto Movement
« Reply #3 on: July 27, 2013, 11:04:31 am »
So I have to downgrade my OS or use linux?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Auto Movement
« Reply #4 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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

HElwy

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: [SOLVED] Auto Movement
« Reply #5 on: July 27, 2013, 09:15:31 pm »
Just had to initialize all the booleans to false