好的,所以要正确地做到这一点,你将不得不重新设计一点。
首先,您应该检查新的游戏手柄输入(即,您应该仅在新按下“A”时才退出屏幕)。为此,您应该存储以前和当前的游戏手柄状态:
private GamePadState currentGamePadState;
private GamePadState lastGamePadState;
// in your constructor
currentGamePadState = new GamePadState();
lastGamePadState = new GamePadState();
// in your update
lastGamePadState = currentGamePadState;
currentGamePadState = GamePad.GetState(PlayerIndex.One);
真正需要做的是修改处理输入的类。HandleInput 函数的基本功能应该移到输入类中。输入应该有一组函数来测试新的/当前的输入。例如,对于您发布的案例:
public Bool IsNewButtonPress(Buttons buton)
{
return (currentGamePadState.IsButtonDown(button) && lastGamePadState.IsButtonUp(button));
}
然后你可以写:
public override void HandleInput(InputState input)
{
if (input.IsNewButtonPress(Buttons.A)
{
this.ExitScreen();
AddScreen(new Background());
}
}
注意:这仅适用于一个控制器。要扩展实现,您需要执行以下操作:
private GamePadState[] currentGamePadStates;
private GamePadState[] lastGamePadStates;
// in your constructor
currentGamePadStates = new GamePadState[4];
currentGamePadStates[0] = new GamePadState(PlayerIndex.One);
currentGamePadStates[1] = new GamePadController(PlayerIndex.Two);
// etc.
lastGamePadStates[0] = new GamePadState(PlayerIndex.One);
// etc.
// in your update
foreach (GamePadState s in currentGamePadStates)
{
// update all of this as before...
}
// etc.
现在,您想要测试每个控制器的输入,因此您需要通过编写一个函数来概括,该函数在检查数组中的每个 GamePadState 是否按下按钮后返回一个 Bool。
Check out the MSDN Game State Management Sample for a well developed implementation. I can't remember if it supports multiple controllers, but the structure is clear and can easily be adapted if not.