2016-04-09 7 views
1

SFMLでC++でゲームをしていて、アイスボールを画面に描画して、画面上で「ウォーキング」したいマウスの左ボタンを押してください。しかし、私がボタンを押すと、アイスボールが描かれません。問題を解決するための答えがありますか?C++/SFMLでマウスボタンを押したときにボールを描く方法

メイン:

#include <iostream> 
#include <SFML/Graphics.hpp> 
#include <string> 

int main(int argc, char* argv[]) 
{ 

    sf::RenderWindow window; 
    window.create(sf::VideoMode(1320, 840), "Game"); 
    window.setFramerateLimit(60); 
    window.setVerticalSyncEnabled(true); 
    sf::Color color(255, 255, 255, 100); 

    sf::Time time = sf::seconds(2); 
    sf::Clock clock; 
    sf::Time time2 = clock.getElapsedTime(); 

    std::cout << time.asSeconds() << std::endl; 
    std::cout << time2.asSeconds() << std::endl; 
    //clock.restart(); 

    //TEXTURE 
    sf::Texture bgTexture; 
    if (!bgTexture.loadFromFile("file", sf::IntRect(0, 0, 1320, 840))) 
    { std::cout << "Error: background not loaded" << std::endl; } 

    sf::Texture pTexture; 
    if (!pTexture.loadFromFile("file", sf::IntRect(0, 0, 32, 32))) 
    { std::cout << "Error: player not loaded" << std::endl; } 

    sf::Texture ibTexture; 
    if (!ibTexture.loadFromFile("file")) 
    { std::cout << "Error: ice-ball not loaded" << std::endl; } 

    //SPRITE 
    sf::Sprite background; 
    background.setTexture(bgTexture); 

    sf::Sprite player; 
    player.setTexture(pTexture); 
    player.setScale(2, 2); 
    player.setPosition(628, 388); 

    sf::Sprite iceBall; 
    iceBall.setTexture(ibTexture); 
    iceBall.setScale(1.5, 1.5); 

    //FONT 
    sf::Font font; 
    if (!font.loadFromFile("file")) 
    { std::cout << "Error: font not loaded" << std::endl; } 

    //TEXT 
    sf::Text commands; 
    commands.setFont(font); 
    commands.setCharacterSize(54); 
    commands.setColor(sf::Color::Black); 
    commands.setString("D=Right \nA=Left \nW=Up \nS=DWON \n(also the arrows)"); 
    commands.setPosition(213, 123); 

    while (window.isOpen()) { 
     sf::Event event; 
     while (window.pollEvent(event)) { 
      if (event.type == sf::Event::Closed) 
      { window.close(); } 

      if (event.type == sf::Event::KeyPressed) { 
       if (event.key.code == sf::Keyboard::D || event.key.code == sf::Keyboard::Right) 
       { player.move(10, 0); } 
       else if (event.key.code == sf::Keyboard::A || event.key.code == sf::Keyboard::Left) 
       { player.move(-10, 0); } 
       else if (event.key.code == sf::Keyboard::W || event.key.code == sf::Keyboard::Up) 
       { player.move(0, -10); } 
       else if (event.key.code == sf::Keyboard::S || event.key.code == sf::Keyboard::Down) 
       { player.move(0, 10); } 
      } 

      /*if (event.type == sf::Event::MouseMoved) 
      { 
       std::cout << "X: " << event.mouseMove.x << " y: " << event.mouseMove.y << std::endl; 
      }*/ 

      if (event.type == sf::Event::MouseButtonPressed) { 
       if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) { 
        std::cout << "Left mouse button pressed" << std::endl; 
        iceBall.setPosition(event.mouseMove.x, event.mouseMove.y); 
        window.draw(iceBall); 
        iceBall.move(event.mouseMove.x++, event.mouseMove.y++); 

       } 
      } 
     } 
     window.clear(color); 

     window.draw(background); 
     window.draw(player); 
     window.draw(commands); 

     window.display(); 
    } 

    return 0; 
} 
+0

関連していません。「setFramerateLimit」と「setVerticalSyncEnabled」の両方を使用しないでください。また、 'sf :: Mouse :: isButtonPressed'を使ってイベントと瞬時探査を混在させないでください。チュートリアルを詳しく見てください。 – Hiura

答えて

2

のは、あなたのプログラムのワークフローを見てみましょう:

  • あなたがイベントを処理し、左ボタンの状態に応じて、あなたはアイスボールを描画します。
  • イベント処理後、画面を消去してさらにいくつかのものを描画します。

したがって、実際にボールを表示することはありません。

代わりに、構造あなたのプログラムは、次のように:

while (window.isOpen) { 
    sf::Event event; 
    while (window.pollEvent(event)) { 
     // Handle event & game *logic* here -- not drawing 
     // e.g. move entities 

     if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) { 
      iceBall.setPosition(event.mouseButton.x, event.mouseButton.y); 
      // CAREFUL HERE: you mixed wrong element of the union event. 
     } 
    } 

    // Draw everything here 
    window.clear(); 
    window.draw(background); 
    window.draw(player); 
    window.draw(iceBall); 
    window.draw(command); 
    window.display(); 
} 

追加注:eventのタイプは、マウスの移動ではありませんでしたが、あなたのコードの中で、あなたはevent.mouseMoveを使用しました。これは無効です。 tutorialアウトチェックアウト、特にこのノート:

再び上の段落を読んで、あなたは完全にそれを理解していることを確認しますが、SF ::イベント組合は、経験の浅いプログラマのための多くの問題の原因です。

関連する問題