2017-07-22 25 views
-1

如何使我的「遊戲」拍攝2個關鍵輸入,例如,如果用戶點擊w並且他向右上移動。 順便說一句,變量eventCheckEvent對象以sfml拍攝2個鍵盤輸入

這裏是我當前的代碼(當然不是全部代碼只是事件代碼):

while (window.isOpen()) { 


Event eventCheck; 
      while (window.pollEvent(eventCheck)) { 
       switch (eventCheck.type) { 
       case Event::Closed: 
        window.close(); 
        break; 
       case Event::KeyPressed: 
        switch (eventCheck.key.code) { 
         case Keyboard::W: 
         if (Keyboard::isKeyPressed(Keyboard::A)) { 
          const Vector2f spritePos = sprite.getPosition(); 
          sprite.setPosition(spritePos.x, spritePos.y - 5);} 
          break; 
         case Keyboard::A: 
         if (Keyboard::isKeyPressed(Keyboard::A)) { 
          const Vector2f spritePos = sprite.getPosition(); 
          sprite.setPosition(spritePos.x - 5, spritePos.y);} 
          break; 
         case Keyboard::S: 
         if (Keyboard::isKeyPressed(Keyboard::S)) { 
          const Vector2f spritePos = sprite.getPosition(); 
          sprite.setPosition(spritePos.x, spritePos.y + 5);} 
          break; 
         case Keyboard::D: 
         if (Keyboard::isKeyPressed(Keyboard::D)) { 
          const Vector2f spritePos = sprite.getPosition(); 
          sprite.setPosition(spritePos.x + 5, spritePos.y);} 
          break; 
            } 
          } 
          break; 
      } 

      window.clear(Color(0,0,0,255)); 

      window.draw(sprite); 
      window.display(); 
    }  
return 0; 
+0

'SF ::鍵盤:: isKeyPressed'是獨立事件。你可以在你的主循環中調用它。 – Gambit

回答

0

1)SFML活動投票通過sf::Event和實時關鍵狀態訪問通過sf::Keyboard提供。如果您只需要處理一個按鍵,最好的方法是通過window.pollEvent輪詢它們,否則通常總是更好地獲得鍵的狀態並根據狀態做出反應。在這種情況下,您將事件輪詢與鍵盤狀態混合在一起。選擇一個或另一個。

2)不使用SF ::從代碼

去掉了很多清晰的現在到代碼!

我不會輪詢任何內部事件的用戶輸入。我會得到鍵的狀態:

// Once per game loop 
void ProcessInput() 
{ 
    int keyCount = 0; 
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) 
    { 
     keyCount++; 
     //Move Character Up, The more keys are pressed, the more i would mess around with speed/velocity here 
    } 
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) 
    { 
     keyCount++; 
     //Move Character Left, The more keys are pressed, the more i would mess around with speed/velocity here 
    } 
     if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) 
    { 

     keyCount++; 
     //Move Character Down, The more keys are pressed, the more i would mess around with speed/velocity here 
    } 
     if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) 
    { 
     keyCount++;  
     //Move Character Right, The more keys are pressed, the more i would mess around with speed/velocity here 
    } 
} 

根據按用戶,你想移動你的性格少,因爲他們可以移動鍵的數量不是垂直/水平更斜,但速度勾搭和速度,你認爲合適。

0

我落得這樣做

bool moveL = false; 
    bool moveU = false; 
    bool moveD = false; 
    bool moveR = false; 
    while (window.isOpen()) { 
     Event eventCheck; 
     while (window.pollEvent(eventCheck)) { 
      switch (eventCheck.type) { 
      case Event::Closed: 
       window.close(); 
       break; 
      case Event::KeyReleased: 
       switch (eventCheck.key.code) { 
       case Keyboard::A: 
        moveL = false; 
        break; 
       case Keyboard::W: 
        moveU = false; 
        break; 
       case Keyboard::S: 
        moveD = false; 
        break; 
       case Keyboard::D: 
        moveR = false; 
        break; 
       } 

       break; 
      case Event::KeyPressed: 
       switch (eventCheck.key.code) { 
       case Keyboard::A: 
        moveL = true; 
        break; 
       case Keyboard::W: 
        moveU = true; 
        break; 
       case Keyboard::S: 
        moveD = true; 
        break; 
       case Keyboard::D: 
        moveR = true; 
        break; 
       case Keyboard::Space: 
        jump(sprite, window); 
        break; 
       } 

       const Vector2f spritePos = sprite.getPosition(); 

       if (moveD && moveR) { 

        sprite.setPosition(spritePos.x + 5, spritePos.y + 5); 
       } 
       else if (moveL && moveU) { 
        sprite.setPosition(spritePos.x - 5, spritePos.y - 5); 

       } 
       else if (moveU && moveD) { 
        sprite.setPosition(spritePos.x, spritePos.y); 
       } 
       else if (moveU && moveR) { 
        sprite.setPosition(spritePos.x + 5, spritePos.y - 5); 
       } 
       else if (moveD && moveL) { 
        sprite.setPosition(spritePos.x - 5, spritePos.y + 5); 
       } 
       else if (moveL) { 
        sprite.setPosition(spritePos.x - 5, spritePos.y); 
       } 
       else if (moveR) { 
        sprite.setPosition(spritePos.x + 5, spritePos.y); 
       } 
       else if (moveU) { 
        sprite.setPosition(spritePos.x, spritePos.y - 5); 
       } 
       else if (moveD) { 
        sprite.setPosition(spritePos.x, spritePos.y + 5); 
       } 



       break; 
      } 
     } 

     window.clear(Color(0,0,0,255)); 

     window.draw(sprite); 
     window.display(); 
    } 
    return 0; 
}