我正在创建一个战舰游戏,我以 A-J x 1-9 的形式输入坐标进行命中,我得到这些并将它们放入列和行变量中,然后在这些坐标处在 2D 数组中输入一个 x。
我的问题是,每当代码进入事件轮询循环时,出于某种原因,它已经认为我按下了一个键并告诉我我已经输入了一行。
我试图使用我编写的用户输入函数来调试它,但无济于事,
注意:我使用的是按键事件而不是文本输入事件,因为我只需要程序识别 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
}
}
}
}
}
}
if
(Event.type == sf::Event::TextEntered)后面的分号,从而有效地跳过了if语句。因此,Event.text.unicode 包含一个不相关的值,在某些情况下,该值会触发以下 if 语句。