Error in playerindex
本文关键字:playerindex in Error | 更新日期: 2023-09-27 18:15:02
我正试图将我的游戏从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中将其用作参数'T'。c:' users ' droo ' dropbox ' games' neoforceccontrols - sharpdx -master'Central'ScreenManager'InputState.cs 95 21 Central错误3:输入"SharpDX"。PlayerIndex'必须是一个非空值类型,以便在泛型类型或方法'System中将其用作参数'T'。' users ' droo ' dropbox ' games' neoforceccontrols - sharpdx -master'Central'ScreenManager'InputState.cs 156 21 Central错误4:输入"SharpDX"。PlayerIndex'必须是一个非空值类型,以便在泛型类型或方法'System中将其用作参数'T'。c:' users ' droo ' dropbox ' games' neoforceccontrols - sharpdx -master'Central'ScreenManager'InputState.cs 172 21 Central错误5:输入"SharpDX"。PlayerIndex'必须是一个非空值类型,以便在泛型类型或方法'System中将其用作参数'T'。' users ' droo ' dropbox ' games' neoforceccontrols - sharpdx -master'Central'ScreenManager'InputState.cs 186 20 Central错误6:输入"SharpDX"。PlayerIndex'必须是一个非空值类型,以便在泛型类型或方法'System中将其用作参数'T'。c:' users ' droo ' dropbox ' games' neoforceccontrols - sharpdx -master'Central'ScreenManager'InputState.cs 201 21 Central错误7输入"SharpDX"。PlayerIndex'必须是一个非空值类型,以便在泛型类型或方法'System中将其用作参数'T'。c:' users ' droo ' dropbox ' games' neoforceccontrols - sharpdx -master'Central'ScreenManager'InputState.cs 216 21 Central错误8无法转换类型"SharpDX"。' users ' droo ' dropbox ' games' neoforceccontrols - sharpdx -master'Central'ScreenManager'InputState.cs 103 23 Central在控制离开当前方法之前,必须将输出参数"playerIndex"分配给C:'Users'Pedro'Dropbox'GAMES' neoforceccontrols - sharpdx -master'Central'ScreenManager'InputState.cs 159错误10:在控制离开当前方法之前,必须将输出参数"playerIndex"分配给C:'Users'Pedro'Dropbox'GAMES' neoforceccontrols - sharpdx -master'Central'ScreenManager'InputState.cs 175 13 Central
PlayerIndex?
表示可以为空或包含值。它是Nullable<PlayerIndex>
的同义词。
您可以将其更改为PlayerIndex
,或者在使用它时将其用作playerIndex.Value
,以便您引用可能包含的值,您也可以在使用它之前检查该类型的HasValue
属性。
关于可空类型的更多信息在这里。
如果没有帮助,试着更具体地说明抛出错误的代码行,而不是复制整个代码。