2016-10-25 101 views
0

我剛開始學習SFML 2.0,遇到了一點問題。問題是,當我按下按鈕「A」,然後在輸出它也顯示「鼠標左按鈕按下」。我使用code :: block 16.04和SFML 2.0。 與「MouseWheelMoved」事件發生同樣的事情,如果我按任何其他數字,然後它顯示消息,「MouseWheelMoved」。這是代碼。當按下鼠標左鍵的同時按下了SFML 2.0

#include <SFML/Graphics.hpp> 
#include <iostream> 
int main() 
{ 
    sf::RenderWindow window(sf::VideoMode(400,400),"All Events "); 
    sf::Event event; 
    window.setKeyRepeatEnabled(false); 
    while(window.isOpen()) 
    { 
     while(window.pollEvent(event)) 
     { 
      switch(event.type) 
      { 
      case sf::Event::Closed: 
       window.close(); 
       break; 
      case sf::Event::KeyPressed: 
       switch(event.key.code) 
       { 
       case sf::Keyboard::A: 
        std::cout<<"A Button Pressed "<<std::endl; 
        break; 
       case sf::Keyboard::S: 
        std::cout<<"S Button Pressed "<<std::endl; 
        break; 
       case sf::Keyboard::D: 
        std::cout<<"D Button Pressed "<<std::endl; 
        break; 
       case sf::Keyboard::W: 
        std::cout<<"W Button Pressed "<<std::endl; 
        break; 
       case sf::Keyboard::Return: 
        std::cout<<"Enter Button Pressed"<<std::endl; 
        break; 
       case sf::Keyboard::Up: 
        std::cout<<"Up Button Pressed "<<std::endl; 
        break; 
       } 
       case sf::Event::MouseButtonPressed: 
        if(event.mouseButton.button == sf::Mouse::Left)  {std::cout<<"Mouse Left Button Pressed "<<std::endl; 
        break;} 
        else if(event.mouseButton.button == sf::Mouse::Right){std::cout<<"Mouse Right Button Pressed"<<std::endl; 
        break;} 
       case sf::Event::MouseWheelMoved: 
        std::cout<<"Mouse Wheel Moved "<<event.mouseWheel.delta<<std::endl; 
        break; 
      } 
     } 
    } 
} 
+3

案件'SF ::活動:: KeyPressed'和'SF ::活動:: MouseButtonPressed'他們中的一個休息的級聯。給它一個_break_。 – Doom5

回答

3

你有兩個switch嵌套,你缺少的

switch(event.type) // switch 1 
{ 
    // (...) 
    case sf::Event::KeyPressed: 
     switch(event.key.code) // switch 2 
     { 
     case sf::Keyboard::A: 
      std::cout<<"A Button Pressed "<<std::endl; 
      break; // <- this breaks switch 2 and... 
     // (...) 
     } 
     // ...you end up here. Here switch 1 doesn't have a break 
     // so it falls through to the next case. 
     case sf::Event::MouseButtonPressed: 
     // (...) 
     // here also falls through. 
     case sf::Event::MouseWheelMoved: 
      std::cout<<"Mouse Wheel Moved "; 
      break; 
}