我需要在我的游戏中做一个菜单。我做到了,但如果我想进入“选项”部分,我不能回到主menu.First的所有我在屏幕上绘制的基本图像(播放,选项和退出),如果计数器是例如0,我绘制另一个图像与另一个不同的效果。看这里(我没有重写所有的代码):
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();
}
}发布于 2014-06-11 21:52:06
首先,我有一些建议可以改进你的输入内容,你可能会觉得有用。我假设这是一个更新函数,因为您似乎首先获取状态并在整个过程中使用它(如果我错了,请告诉我)。
要检查并查看某个键是否被按下一次(未被按住),可以通过存储两个KeyboardStates来实现,如下所示。
// 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;
}
}使用这一小段代码,基本上就是这样做的:
我希望这能澄清你如何去做你的菜单的东西。我唯一遗漏的代码是你的绘图函数。只要弄清楚如何绘制当前菜单,就可以了。祝你好运!
发布于 2014-05-31 20:09:38
您不能检查以下内容:
if(Keyboard.GetState().IsKeyDown(Keys.Enter) && state.IsKeyUp(Keys.Enter))它将始终为false,因为当您声明
KeyboardState state=Keyboard.GetState(); 为什么要使用这个:
Keyboard.GetState().IsKeyDown(Keys.Enter)而不是这个
state.IsKeyDown(Keys.Enter)https://stackoverflow.com/questions/23969857
复制相似问题