首页
学习
活动
专区
圈层
工具
发布

XNA按键
EN

Stack Overflow用户
提问于 2014-05-31 20:04:15
回答 2查看 684关注 0票数 0

我需要在我的游戏中做一个菜单。我做到了,但如果我想进入“选项”部分,我不能回到主menu.First的所有我在屏幕上绘制的基本图像(播放,选项和退出),如果计数器是例如0,我绘制另一个图像与另一个不同的效果。看这里(我没有重写所有的代码):

代码语言:javascript
复制
int count=0;
KeyboardState state=Keyboard.GetState();
if(count <6)//main menu
    {
        //if i press "UP" key count -- 
        //if I press "Down" key count++


        //and i did:
        if(count==0)//and I press "enter"
        {
        //Play
        }
        if(count==1)//and I press "enter"
        {
        // count =6 and go to the opstions
        }
        if(count==2)//and I press "enter"
        {
        //Exit
        }
        //This functions!

    }
        //now i am in Options section
        if(count>5)
        {
            //here I have some options including "back button" which is number 8
        if(count==8)
        {
            if(Keyboard.GetState().IsKeyDown(Keys.Enter) && state.IsKeyUp(Keys.Enter))
        {
            count=1;//and I should move in Main menu BUT IT DOESN'T FUNCTION!! WHY?
        }
         state=Keyboard.GtState();
        }
        }
EN

回答 2

Stack Overflow用户

发布于 2014-06-11 21:52:06

首先,我有一些建议可以改进你的输入内容,你可能会觉得有用。我假设这是一个更新函数,因为您似乎首先获取状态并在整个过程中使用它(如果我错了,请告诉我)。

要检查并查看某个键是否被按下一次(未被按住),可以通过存储两个KeyboardStates来实现,如下所示。

代码语言:javascript
复制
// You need to store these in your class, not locally
KeyboardState m_PreviousState;
KeyboardState m_CurrentState;

接下来,您需要将此代码放入更新函数中:

代码语言:javascript
复制
// At the start of the function, put this
m_CurrentState = Keyboard.GetState();

// do whatever your update function is supposed to do

// At the end of the function, put this
m_PreviousState = m_CurrentState;

这样做的目的是获取每一帧的当前状态,这样您就可以知道键盘当前的样子。通过将最后一帧的状态存储在m_PreviousState中,您现在可以检查并查看是否按下了某个键。要执行此操作,您需要执行以下操作:

代码语言:javascript
复制
if (m_CurrentState.IsKeyUp(Keys.Enter) && m_PreviousState.IsKeyDown(Keys.Enter))
{ /* Run whatever logic */ }

这将解决您的按键问题。至于您的菜单,我建议使用枚举来跟踪您在菜单中的进度。让我来布置一个基本的菜单,以及如何通过编程来实现它。

代码语言:javascript
复制
Play
Load Save
Options
  - Mute Sound
  - Mute Volume
  - Raise / Lower Volume
Quit

让我们假设这是您的菜单(请在您那一端填上您的菜单)。我会将主菜单(播放、加载保存、选项和退出)存储在它自己的枚举中,我会将这些内容存储在选项子菜单中(静音、静音音量、提高/降低音量)它也是自己的枚举。可以这样做,如下所示。

代码语言:javascript
复制
// Putting MAX and MIN in both of them will be useful later, just trust me
public enum MenuOptions
{
    MIN = -1,
    PLAY,
    LOAD_SAVE,
    OPTIONS,
    QUIT,
    MAX
}

public enum OptionsOptions // Yes, i know its not the best name
{
    MIN = -1,
    MUTE_SOUND,
    MUTE_VOLUME,
    RAISE_LOWER_VOLUME,
    MAX
}

// Create your variables in your class at the top
private MenuOptions m_CurrentMenuOption;
private OptionsOptions m_CurrentOptionsOption;
private Bool m_InOptions; // We will need this shortly

现在我们已经为每个菜单和子菜单创建了一个枚举以及用来存储它们的变量,现在我们有了一种简单易读的方法来跟踪我们当前在每个菜单中的位置。现在是我们将它们结合起来的部分。

我通常使用事件处理程序进行键输入,以便它可以独立于update函数运行,但我将假定您想要使用update函数。下面是你可能要做的事情。

代码语言:javascript
复制
// If Enter was just pressed, move into the submenu if it exists
if (m_CurrentState.IsKeyUp(Keys.Enter) && m_PreviousState.IsKeyDown(Keys.Enter))
{
    // If we are currently hovering over options, we should move into that submenu
    if (m_CurrentMenuOption == MenuOptions.OPTIONS)
    {
        // Make sure to reset all other bools like this if you have any to avoid
        // confusing bugs later
        m_InOptions = true;
    }
}
// If Down was pressed, move the enum value we store
else if (m_CurrentState.IsKeyUp(Keys.Down) && m_PreviousState.IsKeyDown(Keys.Down))
{
    // Check to see if we are in options submenu so we can move that value rather than 
    // the main menu's value
    if (m_InOptions)
    {
        m_CurrentOptionsOption++;

        // Checks to see if you went further than the last option and sets it to the
        // last option if you did
        if ((int)m_CurrentOptionsOption >= (int)OptionsOptions.MAX)
            m_CurrentOptionsOption = OptionsOptions.RAISE_LOWER_VOLUME;
    }
    else // You could add some else ifs before for other sub menus
    {
        // Since we are not in a submenu, update the current menu option
        m_CurrentMenuOptions++;

        // Checks to see if you went further than the last option and sets it to the
        // last option if you did
        if ((int)m_CurrentMenuOptions >= (int)MenuOptions.MAX)
            m_CurrentMenuOption = MenuOptions.QUIT;
    }
}
// If Up was pressed, move the enum value we store
else if (m_CurrentState.IsKeyUp(Keys.Up) && m_PreviousState.IsKeyDown(Keys.Up))
{
    // Check to see if we are in options submenu so we can move that value rather than
    // the main menu's value
    if (m_InOptions)
    {
        m_CurrentOptionsOption--;

        // Checks to see if you went further than the first option and sets it to the
        // first option if you did
        if ((int)m_CurrentOptionsOption <= (int)OptionsOptions.MIN)
            m_CurrentOptionsOption = OptionsOptions.MUTE_SOUND;
    }
    else // You could add some else ifs before for other sub menus
    {
        // Since we are not in a submenu, update the current menu option
        m_CurrentMenuOptions--;

        // Checks to see if you went further than the first option and sets it to the
        // first option if you did
        if ((int)m_CurrentMenuOptions <= (int)MenuOptions.MIN)
            m_CurrentMenuOption = MenuOptions.PLAY;
    }
}
// If Backspace was pressed, move back a menu if possible
else if (m_CurrentState.IsKeyUp(Keys.Back) && m_PreviousState.IsKeyDown(Keys.Back))
{
    if (m_InOptions) // you would tack on some || or else ifs for any other submenus
    {
        m_InOptions = false;
    }
}

使用这一小段代码,基本上就是这样做的:

  1. 检查是否按下了enter键。如果是,您将进入一个子菜单(如果它存在)。如果按下了needed.
  2. Checking,您可以在那里进一步添加代码来更改菜单,以查看是否已按下。如果是,则在当前的menu.
  3. Checking上向下移动,看看是否按下了up。如果是,则在当前menu.
  4. Checking上移以查看是否按下了backspace。如果是,则向后移动一个菜单(如果可能)。

我希望这能澄清你如何去做你的菜单的东西。我唯一遗漏的代码是你的绘图函数。只要弄清楚如何绘制当前菜单,就可以了。祝你好运!

票数 2
EN

Stack Overflow用户

发布于 2014-05-31 20:09:38

您不能检查以下内容:

代码语言:javascript
复制
if(Keyboard.GetState().IsKeyDown(Keys.Enter) && state.IsKeyUp(Keys.Enter))

它将始终为false,因为当您声明

代码语言:javascript
复制
KeyboardState state=Keyboard.GetState(); 

为什么要使用这个:

代码语言:javascript
复制
Keyboard.GetState().IsKeyDown(Keys.Enter)

而不是这个

代码语言:javascript
复制
state.IsKeyDown(Keys.Enter)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23969857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档