2017-01-25 17 views
0

私はSFMLライブラリでゲームを作っています。私はPlayerを動かそうとしています。右矢印キーを押しながら移動していない理由を私は知らない。SFMLプレーヤーの移動の問題

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); 
} 

は、私は自分のコードに変更すべきかを教えてお気軽に。

答えて

0

は、だからここにあなたのメインループの中で何が起こっているのである:

character.SetPosition(windowWidth, windowHeight); // Put this line to get a start location 
    while (window.isOpen()) 
    { 
     sf::Event e; 
     while (window.pollEvent(e)) 
     { 
      // your code... 
     } 

     character.SetPosition(windowWidth, windowHeight); // <---- Remove this line 
     character.UpdatePosition(); 
     Draw(); 
    } 
} 

あなたは常にwindowWidth、およびwindowHeightに文字位置を設定しているので、関係なく、あなたがcharacter.MoveRight()を呼び出し、あなたは常に位置をresetingています。

私もそれが複数回実行することができますし、あなたのcharacter.MoveRight()複数回ヒットするよう、多分update方法であなたのPlayerの内側にそれらを配置し、あなたのイベントループのためにそれらを削除し、入力コントロールを処理するために何かを追加することをお勧めします。

最後のアドバイスはSFMLの時計を調べるので、フレームレートの代わりに時間に基づいてスムーズに文字を移動できます。

関連する問題