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

Author Topic: Player movement issue  (Read 776 times)

0 Members and 1 Guest are viewing this topic.

erzis

  • Newbie
  • *
  • Posts: 1
    • View Profile
Player movement issue
« on: January 25, 2017, 09:36:31 pm »
Hello,
I'm trying to make a game in SFML, but my Player doesn't move when i click the Right Arrow. What's the cause?

Game.cpp

#include "Game.h"

Game::Game()
{
        windowWidth = 800;
        windowHeight = 600;
}

Game::~Game()
{
}

void Game::Start()
{
        window.create(sf::VideoMode(windowWidth, windowHeight), "Game");
        window.setFramerateLimit(60);

        while (window.isOpen())
        {
                sf::Event e;
                while (window.pollEvent(e))
                {
                        if (e.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        {
                                window.close();
                        }
                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                        }
                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                character.MoveRight();
                        }
                }

                character.SetPosition(windowWidth, windowHeight);
                character.UpdatePosition();
                Draw();
        }
}

void Game::Draw()
{
        window.clear();

        character.DrawPlayer(window);

        window.display();
}
 


Player.cpp

#include "Player.h"

Player::Player()
{
        player.setSize(sf::Vector2f(200, 50));
        player.setFillColor(sf::Color::White);

        playerX = 300;
        playerY = 300;
        playerSpeed = 5.f;
}

Player::~Player()
{
}

void Player::MoveRight()
{
        playerX += playerSpeed;
}

void Player::SetPosition(float windowWidth, float windowHeight)
{
        playerX = windowWidth / 2 - 100;
        playerY = windowHeight - 50;
}

void Player::UpdatePosition()
{
        player.setPosition(playerX, playerY);
}

void Player::DrawPlayer(sf::RenderWindow &window)
{
        window.draw(player);
}
 
« Last Edit: January 25, 2017, 10:31:18 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Player movement issue
« Reply #1 on: January 25, 2017, 10:33:55 pm »
You set the position every game loop iteration, so any change you make in moveRight you make to playerX is discarded.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything