【问题标题】:xna how to setup controls in gamexna如何在游戏中设置控件
【发布时间】:2014-08-05 12:50:21
【问题描述】:

我希望能够进入我正在开发的游戏中的选项菜单并设置我的控件。 这是一个简单的乒乓球游戏(目前),每个玩家的控件都只是上下。

这就是我希望该过程看起来的样子:我单击 SETUP CONTROLS,游戏显示我应该更改的控件的名称并等待,我单击键盘上我希望将其更改为的按钮,游戏读取它并显示我应该更改的下一个控件,依此类推,直到我更改所有控件。

我找到了一种方法来做到这一点here,我的代码现在看起来基本上是这样的:

if (optionsBList.IsButtonClicked("SETUP CONTROLS")) //when i click the 
                                                    //SETUP CONTROLS button
                                                    //in the options menu
{
    KeyboardState currentKeyboardState =  new KeyboardState();
    waitingForKey = true;
    while (waitingForKey)
    {
        if(currentKeyboardState.GetPressedKeys().Count() > 0)
        {
            player1.upkey = currentKeyboardState.GetPressedKeys()[0];
            //the path for the key isn't player1.upkey, but lets say it is.
            waitingForKey = false;
         }
    }
 }

在这个简短的代码中,我的目标是只更改一个键。如果我可以让它更改 1 个键,那么更改更多将不是问题。

问题是,当我单击设置控制按钮时,我不明白为什么我的游戏停止响应。我在这里没有看到无限循环,也没有看到内存泄漏。 为什么我的游戏崩溃了,有没有更好的方法来加载选项菜单中的控件?

【问题讨论】:

  • while(waitingForKey) 是你的无限循环。该循环阻塞了游戏线程,因此无法识别进一步的输入。只要在每次更新时执行此代码,将 while 更改为 if 应该可以工作。

标签: menu xna key controls installation


【解决方案1】:

如果您从问题中的链接中看到答案,您会注意到他实际上删除了 while 循环并将其设为 if 语句。

就像 Nico Schertler 说的:“while(waitingForKey) 是你的无限循环。那个循环阻塞了游戏线程,所以没有进一步的输入被识别。”

您的链接中的答案链接:https://stackoverflow.com/a/15935732/3239917

if (optionsBList.IsButtonClicked("SETUP CONTROLS")) //when i click the 
                                                    //SETUP CONTROLS button
                                                    //in the options menu
{
    KeyboardState currentKeyboardState =  new KeyboardState();
    waitingForKey = true;
    if (waitingForKey )
    {
        if(currentKeyboardState.GetPressedKeys().Count() > 0)
        {
            player1.upkey = currentKeyboardState.GetPressedKeys()[0];
            //the path for the key isn't player1.upkey, but lets say it is.
            waitingForKey = false;
         }
    }
 }

【讨论】:

  • 好吧,现在它没有崩溃,但它不能正常工作。我没有提到这种和平的代码会不断更新,对此我深表歉意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多