2015-01-19 5 views
1

Я создаю игру с линкорами, в которой я ввожу координаты в форме AJ x 1-9 для моего удара, и я получаю их и помещаю их в переменную столбца и строки, а затем вводя x в 2D-массив в этих координатах.C++ SFML ошибка символов на клавиатуре

Моя проблема заключается в том, что всякий раз, когда код входит в цикл опроса событий, по какой-то причине он уже думает, что я нажал клавишу и сказал, что я ввел строку.

Я пытался играть с помощью функции пользовательского ввода я написал для отладки это, без толку,

Примечание: Я использую Keypressed событие а не textentered событие, как мне нужно только программу распознавать 1 ключ,

здесь соответствующий код:

void GetUserInput() 
{ 
     sf::Event Event; // create an event instance 

     std::cout << "waiting for input" << std::endl; // output to console  for debugging purposes 

     while(bTurnTaken == false) 
     { 
      while (menu.pollEvent(Event)) // create a polling loop that  polls the event instance 
      { 
       if(bRowInputted == false) // if the row has not been entered 
       { 
        if(Event.type == sf::Event::TextEntered); // if there is  a TextEntered event (i.e the user enters the coordinates they would like to hit  for their turn) 
        { 
         if(Event.text.unicode >= 32 && Event.text.unicode <= 126) // if what was entered was a valid character 
         { 
          cRow = static_cast<char>(Event.text.unicode); // take what was entered and store it in the variable iRow 
          ConvertRow(); // calls the function to convert the entered row from A-J format to 0-10 
          std::cout << "Row chosen: " << iRowConverted << std::endl; // output to console for debugging 
          bRowInputted = true; // tells the program the row has been inputted so to move on to getting the column 
         } 

        } 
       } 

       else if(bRowInputted == true) // if the row has been entered 
       { 
        if(Event.type == sf::Event::TextEntered); // if there is a TextEntered event (i.e the user enters the coordinates they would like to hit for their turn) 
        { 
         if(Event.text.unicode >= 32 && Event.text.unicode <= 126) // if what was entered was a valid character 
         { 
          iColumn = static_cast<char>(Event.text.unicode); // take what was entered and store it in the variable iRow 
          iColumn = iColumn - 49; // converts the ascii value of the pressed character into decimal, also changes the entered 1-10 into 0-9 
          std::cout << "Column chosen: " << iColumn << std::endl; // output to console for debugging 
          bTurnTaken = true; // signal the end of the turn as the program now has desired row and column 
         } 

        } 
       } 
      } 
     } 
} 
+0

Вы должны сообщить нам, где именно проблема. Как вы знаете, «он уже думает, что я нажал ключ», каким образом это проявляется? Кроме того, вы правильно инициализируете все переменные (особенно логические)? – Lukas

ответ

0

Semi-двоеточие после если (Event.type == SF :: Event :: TextEntered), таким образом, эффективно пропускать, что если заявление. Поэтому Event.text.unicode содержит несвязанное значение, которое в некоторых случаях вызывает следующий оператор if.