我正试图将我的游戏从XNA移植到SharpDX,但我无法找到解决问题的方法:
namespace TomShane.Neoforce.Central
{
public class InputState
{
#region Fields
public const int MaxInputs = 4;
public readonly KeyboardState[] CurrentKeyboardStates;
// public readonly GamePadState[] CurrentGamePadStates;
public readonly KeyboardState[] LastKeyboardStates;
// public readonly GamePadState[] LastGamePadStates;
public readonly KeyboardManager Keyboard;
// public readonly bool[] GamePadWasConnected;
// public TouchCollection TouchState;
//public readonly List<GestureSample> Gestures = new List<GestureSample>();
#endregion
#region Initialization
/// <summary>
/// Constructs a new input state.
/// </summary>
public InputState()
{
CurrentKeyboardStates = new KeyboardState[MaxInputs];
// CurrentGamePadStates = new GamePadState[MaxInputs];
LastKeyboardStates = new KeyboardState[MaxInputs];
// LastGamePadStates = new GamePadState[MaxInputs];
//GamePadWasConnected = new bool[MaxInputs];
}
#endregion
#region Public Methods
/// <summary>
/// Reads the latest state of the keyboard and gamepad.
/// </summary>
public void Update()
{
for (int i = 0; i < MaxInputs; i++)
{
LastKeyboardStates[i] = CurrentKeyboardStates[i];
// LastGamePadStates[i] = CurrentGamePadStates[i];
CurrentKeyboardStates[i] = Keyboard.GetState();
// CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
// Keep track of whether a gamepad has ever been
// connected, so we can detect if it is unplugged.
//if (CurrentGamePadStates[i].IsConnected)
//{
// GamePadWasConnected[i] = true;
//}
}
// TouchState = TouchPanel.GetState();
//Gestures.Clear();
//while (TouchPanel.IsGestureAvailable)
//{
// Gestures.Add(TouchPanel.ReadGesture());
//}
}
/// <summary>
/// Helper for checking if a key was newly pressed during this update. The
/// controllingPlayer parameter specifies which player to read input for.
/// If this is null, it will accept input from any player. When a keypress
/// is detected, the output playerIndex reports which player pressed it.
/// </summary>
public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer,
out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
// Read input from the specified player.
playerIndex = controllingPlayer.Value;
int i = (int)playerIndex;
// int i = playerIndex;
return (CurrentKeyboardStates[i].IsKeyDown(key) &&
LastKeyboardStates[i].IsKeyPressed(key));
//IsKeyUp(key));
}
else
{
// Accept input from any player.
return (IsNewKeyPress(key, PlayerIndex.One, out playerIndex)); //||
// IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) ||
// IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) ||
// IsNewKeyPress(key, PlayerIndex.Four, out playerIndex));
}
}
/// <summary>
/// Helper for checking if a button was newly pressed during this update.
/// The controllingPlayer parameter specifies which player to read input for.
/// If this is null, it will accept input from any player. When a button press
/// is detected, the output playerIndex reports which player pressed it.
/// </summary>
/// <summary>
/// Checks for a "menu select" input action.
/// The controllingPlayer parameter specifies which player to read input for.
/// If this is null, it will accept input from any player. When the action
/// is detected, the output playerIndex reports which player pressed it.
/// </summary>
public bool IsMenuSelect(PlayerIndex? controllingPlayer,
out PlayerIndex playerIndex)
{
return IsNewKeyPress(Keys.Space, controllingPlayer, out playerIndex) ||
IsNewKeyPress(Keys.Enter, controllingPlayer, out playerIndex);
}
/// <summary>
/// Checks for a "menu cancel" input action.
/// The controllingPlayer parameter specifies which player to read input for.
/// If this is null, it will accept input from any player. When the action
/// is detected, the output playerIndex reports which player pressed it.
/// </summary>
public bool IsMenuCancel(PlayerIndex? controllingPlayer,
out PlayerIndex playerIndex)
{
return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex);
}
/// <summary>
/// Checks for a "menu up" input action.
/// The controllingPlayer parameter specifies which player to read
/// input for. If this is null, it will accept input from any player.
/// </summary>
public bool IsMenuUp(PlayerIndex? controllingPlayer)
{
PlayerIndex playerIndex;
return IsNewKeyPress(Keys.Up, controllingPlayer, out playerIndex);
}
/// <summary>
/// Checks for a "menu down" input action.
/// The controllingPlayer parameter specifies which player to read
/// input for. If this is null, it will accept input from any player.
/// </summary>
public bool IsMenuDown(PlayerIndex? controllingPlayer)
{
PlayerIndex playerIndex;
return IsNewKeyPress(Keys.Down, controllingPlayer, out playerIndex);// ||
// IsNewButtonPress(Buttons.DPadDown, controllingPlayer, out playerIndex) ||
// IsNewButtonPress(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex);
}
/// <summary>
/// Checks for a "pause the game" input action.
/// The controllingPlayer parameter specifies which player to read
/// input for. If this is null, it will accept input from any player.
/// </summary>
public bool IsPauseGame(PlayerIndex? controllingPlayer)
{
PlayerIndex playerIndex;
return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex);// ||
// IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex) ||
// IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
}
#endregion
}
}
我收到了这个错误。我不知道如何解决它。
错误2类型'SharpDX.PlayerIndex'必须是非可空值类型才能在泛型类型或方法'System.Nullable'C:\ Users \ Pedro \ Dropbox \中将其用作参数'T' GAMES \ NeoforceControls-SharpDX-master \ Central \ ScreenManager \ InputState.cs 95 21 Central 错误3类型'SharpDX.PlayerIndex'必须是非可空值类型才能在泛型类型或方法'System.Nullable'中使用它作为参数'T'C:\ Users \ Pedro \ Dropbox \ GAMES \ NeoforceControls -SharpDX-master \ Central \ ScreenManager \ InputState.cs 156 21 Central 错误4类型'SharpDX.PlayerIndex'必须是非可空值类型才能在泛型类型或方法'System.Nullable'C:\ Users \ Pedro \ Dropbox \ GAMES \ NeoforceControls中将其用作参数'T' -SharpDX-master \ Central \ ScreenManager \ InputState.cs 172 21 Central 错误5类型'SharpDX.PlayerIndex'必须是非可空值类型才能在泛型类型或方法'System.Nullable'C:\ Users \ Pedro \ Dropbox \ GAMES \ NeoforceControls中将其用作参数'T' -SharpDX-master \ Central \ ScreenManager \ InputState.cs 186 20 Central 错误6类型'SharpDX.PlayerIndex'必须是非可空值类型才能在泛型类型或方法'System.Nullable'C:\ Users \ Pedro \ Dropbox \ GAMES \ NeoforceControls中将其用作参数'T' -SharpDX-master \ Central \ ScreenManager \ InputState.cs 201 21 Central 错误7类型'SharpDX.PlayerIndex'必须是非可空值类型才能在泛型类型或方法'System.Nullable'中使用它作为参数'T'C:\ Users \ Pedro \ Dropbox \ GAMES \ NeoforceControls -SharpDX-master \ Central \ ScreenManager \ InputState.cs 216 21 Central 错误8无法将类型'SharpDX.PlayerIndex'转换为'int'C:\ Users \ Pedro \ Dropbox \ GAMES \ NeoforceControls-SharpDX-master \ Central \ ScreenManager \ InputState.cs 103 23 Central 错误9必须在控件离开当前方法C之前分配out参数'playerIndex':\ Users \ Pedro \ Dropbox \ GAMES \ NeoforceControls-SharpDX-master \ Central \ ScreenManager \ InputState.cs 159 13 Central 错误10必须在控件离开当前方法C之前指定out参数'playerIndex':\ Users \ Pedro \ Dropbox \ GAMES \ NeoforceControls-SharpDX-master \ Central \ ScreenManager \ InputState.cs 175 13 Central
答案 0 :(得分:1)
PlayerIndex?
表示它可以为null或包含值。它是Nullable<PlayerIndex>
的同义词。
您可以将其更改为PlayerIndex
,或者在使用时将其用作playerIndex.Value
,以便您参考可能包含的值,您也可以查看如果是这种情况,请在检查类型的HasValue
属性之前使用它。
有关可空类型here的更多信息。
如果这没有帮助,请尝试更具体地说明抛出错误的代码行,而不是复制整个错误。