Je fais un jeu dans la bibliothèque SFML et j'essaie de faire le Player
déplacer. Je n'ai aucune idée pourquoi il ne bouge pas tout en appuyant sur la touche fléchée droite.SFML Player mouvement numéro
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);
}
Ne hésitez pas à me dire ce que dois-je changer dans mon code.