c ++ SFML keypressed字符错误

时间:2015-01-19 01:23:15

标签: c++ input user-input sfml

我正在创建一个战舰游戏,我按照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
                        }

                    }
                }
            }
        }
}

1 个答案:

答案 0 :(得分:0)

if之后的分号(Event.type == sf :: Event :: TextEntered)从而有效地跳过if语句。 因此,Event.text.unicode包含一个不相关的值,在某些情况下,它会触发以下if语句。