C# XNA按键

C# XNA按键,c#,xna,xna-4.0,C#,Xna,Xna 4.0,我需要在游戏中制作菜单。我这样做了,但是如果我想进入“选项”部分,我不能返回主菜单。首先,我在屏幕上绘制了基本图像(播放、选项和退出),例如,如果计数器为0,我在另一个图像的上方绘制另一个具有不同效果的图像。看这里(我没有重写所有的代码): int count=0; KeyboardState=Keyboard.GetState(); 如果(计数5) { //这里我有一些选项,包括“后退按钮”,它是数字8 如果(计数=8) { if(Keyboard.GetState().IsKeyDown(K

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

int count=0;
KeyboardState=Keyboard.GetState();
如果(计数5)
{
//这里我有一些选项,包括“后退按钮”,它是数字8
如果(计数=8)
{
if(Keyboard.GetState().IsKeyDown(Keys.Enter)和&state.iskeydup(Keys.Enter))
{
count=1;//我应该在主菜单中移动,但它不起作用!!为什么?
}
state=Keyboard.GtState();
}
}

您不能检查此项:

if(Keyboard.GetState().IsKeyDown(Keys.Enter) && state.IsKeyUp(Keys.Enter))
它将始终为false,因为当您声明

KeyboardState state=Keyboard.GetState(); 
为什么要使用这个:

Keyboard.GetState().IsKeyDown(Keys.Enter)
而不是这个

state.IsKeyDown(Keys.Enter)

您不能检查以下内容:

if(Keyboard.GetState().IsKeyDown(Keys.Enter) && state.IsKeyUp(Keys.Enter))
它将始终为false,因为当您声明

KeyboardState state=Keyboard.GetState(); 
为什么要使用这个:

Keyboard.GetState().IsKeyDown(Keys.Enter)
而不是这个

state.IsKeyDown(Keys.Enter)

您不能检查以下内容:

if(Keyboard.GetState().IsKeyDown(Keys.Enter) && state.IsKeyUp(Keys.Enter))
它将始终为false,因为当您声明

KeyboardState state=Keyboard.GetState(); 
为什么要使用这个:

Keyboard.GetState().IsKeyDown(Keys.Enter)
而不是这个

state.IsKeyDown(Keys.Enter)

您不能检查以下内容:

if(Keyboard.GetState().IsKeyDown(Keys.Enter) && state.IsKeyUp(Keys.Enter))
它将始终为false,因为当您声明

KeyboardState state=Keyboard.GetState(); 
为什么要使用这个:

Keyboard.GetState().IsKeyDown(Keys.Enter)
而不是这个

state.IsKeyDown(Keys.Enter)

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

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

// You need to store these in your class, not locally
KeyboardState m_PreviousState;
KeyboardState m_CurrentState;
接下来,您将希望将其放入更新函数中:

// 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中,您现在可以检查并查看是否按下了键。要执行此操作,您需要执行以下操作:

if (m_CurrentState.IsKeyUp(Keys.Enter) && m_PreviousState.IsKeyDown(Keys.Enter))
{ /* Run whatever logic */ }
这将解决您的按键问题。至于你的菜单,我建议使用枚举来跟踪你在菜单中的进度。让我来布置一个基本菜单,以及如何以编程方式进行

Play
Load Save
Options
  - Mute Sound
  - Mute Volume
  - Raise / Lower Volume
Quit
让我们假设这是你的菜单(请用你的菜单填写)。我会将主菜单(播放、加载保存、选项和退出)存储在自己的枚举中,并将内容存储在选项子菜单(静音、静音音量、提高/降低音量)中,这也是自己的枚举。这可以按如下所示完成

// 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函数。下面是你可以如何着手做这件事

// 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;
    }
}
//如果刚刚按下Enter键,请移入子菜单(如果存在)
if(m_CurrentState.IsKeyUp(key.Enter)和&m_PreviousState.IsKeyDown(key.Enter))
{
//如果我们当前悬停在选项上,我们应该进入该子菜单
if(m_CurrentMenuOption==MenuOptions.OPTIONS)
{
//如果您有任何需要避免的问题,请确保重置所有其他布尔值
//以后再弄糊涂虫子
m_操作=真;
}
}
//如果按下向下键,则移动存储的枚举值
else if(m_CurrentState.IsKeyUp(Keys.Down)和&m_PreviousState.IsKeyDown(Keys.Down))
{
//检查是否在选项子菜单中,以便移动该值而不是
//主菜单的值
如果(m_操作)
{
m_CurrentOptions Option++;
//检查是否超出上一个选项,并将其设置为
//最后一个选择,如果你这样做了
如果((int)m_currentOptions Option>=(int)options options.MAX)
m_CurrentOptions Option=选项Options.Rise_LOWER_VOLUME;
}
else//您可以在其他子菜单之前添加其他ifs
{
//由于我们不在子菜单中,请更新当前菜单选项
m_CurrentMenuOptions++;
//检查是否超出上一个选项,并将其设置为
//最后一个选择,如果你这样做了
如果((int)m_CurrentMenuOptions>=(int)MenuOptions.MAX)
m_CurrentMenuOptions=MenuOptions.QUIT;
}
}
//如果按Up键,则移动存储的枚举值
else if(m_CurrentState.IsKeyUp(Keys.Up)和&m_PreviousState.IsKeyDown(Keys.Up))
{
//检查是否在选项子菜单中,以便移动该值而不是
//主菜单的值
如果(m_操作)
{
m_当前选项选项选项--;
//检查是否超出了第一个选项,并将其设置为
//第一个选择,如果你这样做了

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

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

// You need to store these in your class, not locally
KeyboardState m_PreviousState;
KeyboardState m_CurrentState;
接下来,您将希望将其放入更新函数中:

// 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中,您现在可以检查并查看是否按下了键。要执行此操作,您需要执行以下操作:

if (m_CurrentState.IsKeyUp(Keys.Enter) && m_PreviousState.IsKeyDown(Keys.Enter))
{ /* Run whatever logic */ }
这会解决你的问题