这个问题很奇怪,为此我道歉。我正在努力创造一个小游戏,而且我很难用一台状态机。
枚举类型
public enum Phase
{
FirstPhase,
SecondPhase,
ThirdPhase,
FourthPhase;
};
代码
public Phase currentPhase = Phase.FirstPhase;
public KeyboardState keyboardState;
public KeyboardState oldKeyboardState;
void Update(GameTime gameTime)
{
oldKeyboardState = keyboardState;
keyboardState = Keyboard.GetState();
switch (currentPhase)
{
case FirstPhase:
//just throws the phase to be secondPhase
currentPhase = Phase.SecondPhase;
break;
case SecondPhase:
//wait for input
if (keyboardState.IsKeyDown(Keys.Enter) && oldKeyboardState.IsKeyUp(Keys.Enter))
{
currentPhase = Phase.ThirdPhase;
}
break;
case ThirdPhase:
//do some calculation stuff, decide on something, etc.
if (keyboardState.IsKeyDown(Keys.Enter) && oldKeyboardState.IsKeyUp(Keys.Enter))
{
currentPhase = Phase.FourthPhase;
}
break;
case FourthPhase:
//end of phases, exit or do nothing, etc.
break;
}
}
问题发生在SecondPhase
进入第三阶段时,似乎总是想跳过ThirdPhase
并直接转到FourthPhase
,因为输入仍然被按下,但我想它应该只做一个"阶段"在Update
的一次通话中最多,所以在您再次按 之前不应该转到FourthPhase
?
答案 0 :(得分:1)
最简单的解决方案是翻转键上/下逻辑。
if (keyboardState.IsKeyUp(Keys.Enter) && oldKeyboardState.IsKeyDown(Keys.Enter))
{
currentPhase = Phase.ThirdPhase;
}
这应该具有仅在用户按下后释放回车键时改变状态的效果。
它会有一种略微不同的感觉"对于输入,但如果你能接受它,这是一个很好的简单解决方案。