【问题标题】:c++ SFML keypressed character errorc++ SFML按键字符错误
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-08-26
    • 1970-01-01
    • 2014-11-15
    • 2022-11-14
    • 2014-03-16
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2020-01-29
    相关资源
    最近更新 更多