1
General / 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
Player.cpp
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();
}
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);
}
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);
}