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

Author Topic: Circle not moving correctly, despite following tutorial?  (Read 1907 times)

0 Members and 1 Guest are viewing this topic.

KaliAhmed

  • Newbie
  • *
  • Posts: 7
    • View Profile
Circle not moving correctly, despite following tutorial?
« on: April 04, 2015, 01:06:54 am »
I'm currently reading a PDF on SFML game coding and despite following everything, my code won't work. It compiles and shows a circle, but if I move it, it disappears!! What am I doing wrong?

#include <SFML\Graphics.hpp>

class Game
{
public:
        Game();
        void run();
private:
        void processEvents();
        void update(sf::Time);
        void render();
        void handlePlayerInput(sf::Keyboard::Key, bool);
private:
        sf::RenderWindow mWindow;
        sf::CircleShape mPlayer;
        float PlayerSpeed;
        bool mIsMovingUp, mIsMovingDown, mIsMovingLeft, mIsMovingRight;
        sf::Time deltaTime;
        const sf::Time TimePerFrame = sf::seconds(1.f / 60.f);
};

Game::Game():
mWindow(sf::VideoMode(640, 480), "SFML Application"), mPlayer()
{
        mPlayer.setRadius(40.f);
        mPlayer.setPosition(100.f, 100.f);
        mPlayer.setFillColor(sf::Color::Cyan);
}
void Game::run()
{
        sf::Clock clock;
        sf::Time timeSinceLastUpdate = sf::Time::Zero;
        while (mWindow.isOpen())
        {
                processEvents();
                timeSinceLastUpdate += clock.restart();
                while (timeSinceLastUpdate > TimePerFrame)
                {
                        timeSinceLastUpdate -= TimePerFrame;
                        processEvents();
                        update(TimePerFrame);
                }
                render();
        }
}

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

void Game::processEvents()
{
        sf::Event event;
        while (mWindow.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:
                        mWindow.close();
                        break;
                }
        }
}

void Game::update(sf::Time deltaTime)
{
        sf::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();
}

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: Circle not moving correctly, despite following tutorial?
« Reply #1 on: April 04, 2015, 01:38:01 am »
I don't see you ever giving a value to PlayerSpeed. It's value is probably going to be quite large, moving your circle by a large amount, out of the window.

KaliAhmed

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Circle not moving correctly, despite following tutorial?
« Reply #2 on: April 04, 2015, 01:43:09 am »
I gave it 0.0f value, but now it wont move..

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Circle not moving correctly, despite following tutorial?
« Reply #3 on: April 04, 2015, 02:41:05 am »
Uh? It won't move when you set speed to 0? Surprising! ;)

KaliAhmed

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Circle not moving correctly, despite following tutorial?
« Reply #4 on: April 04, 2015, 01:04:07 pm »
G, do you want to get married? :P Thanks so much!