【发布时间】:2015-01-19 01:23:14
【问题描述】:
我正在创建一个战舰游戏,在该游戏中我以 AJ x 1-9 的形式输入我的命中坐标,我得到这些并将它们放入列和行变量中,然后在二维数组中输入一个 x坐标。
我的问题是,每当代码进入事件轮询循环时,由于某种原因它已经认为我按下了一个键并告诉我我输入了一行。
我尝试使用我编写的用于调试的用户输入函数但无济于事,
注意:我使用的是 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
}
}
}
}
}
}
【问题讨论】:
-
您需要告诉我们问题出在哪里。你怎么知道“它已经认为我按下了一个键”这体现在什么方面?此外,您是否正确初始化了所有变量(尤其是布尔值)?
标签: c++ input user-input sfml