Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PlayerIndex和PlayerIndex?_C#_Input_Xna - Fatal编程技术网

C# PlayerIndex和PlayerIndex?

C# PlayerIndex和PlayerIndex?,c#,input,xna,C#,Input,Xna,我有一个关于PlayerIndex以及如何使用它的问题 问题是第二个控制器(controllingPlayer2)有时需要为“null”(例如,对于一个播放器模式),但GetState不能接受null变量 我有这样一个类(用于我的游戏屏幕): public GameScreen(PlayerIndex? controllingPlayer, PlayerIndex? controllingPlayer2) { this.ControllingPlayer = co

我有一个关于PlayerIndex以及如何使用它的问题

问题是第二个控制器(controllingPlayer2)有时需要为“null”(例如,对于一个播放器模式),但GetState不能接受null变量

我有这样一个类(用于我的游戏屏幕):

    public GameScreen(PlayerIndex? controllingPlayer, PlayerIndex? controllingPlayer2)
    {
        this.ControllingPlayer = controllingPlayer;
        this.ControllingPlayer2 = controllingPlayer2;
    }

    public override void HandleInput()
    {
        // Move the character UP - Problem appears at the GetState Here wanting a PlayerIndex not a null?
        if (GamePad.GetState(ControllingPlayer).IsButtonDown(controls.BUp))
        {
            P1movementY--;
        }

        // Move the second character UP - Problem appears at the GetState Here wanting a PlayerIndex not a null?

        if (GamePad.GetState(ControllingPlayer).IsButtonDown(controls.BUp))
        {
            P2movementY--;
        }

与&&一起使用,以在早期阻塞时短路

    if (ControllingPlayer2 != null && GamePad.GetState(ControllingPlayer2).IsButtonDown(controls.BUp))
    {
        P2movementY--;
    }

与&&一起使用,以在早期阻塞时短路

    if (ControllingPlayer2 != null && GamePad.GetState(ControllingPlayer2).IsButtonDown(controls.BUp))
    {
        P2movementY--;
    }
简短回答,检查(ControllingPlayer!=null),然后使用ControllingPlayer.Value

更长的形式, C#中的问号语法允许将null不是可赋值值的值类型传递为null。它是Nullable的缩写,它有两个属性,比如HasValue:bool和Value:PlayerIndex。它实际上与该类型的不可为null的版本不同

简短回答,检查(ControllingPlayer!=null),然后使用ControllingPlayer.Value

更长的形式,
C#中的问号语法允许将null不是可赋值值的值类型传递为null。它是Nullable的缩写,它有两个属性,比如HasValue:bool和Value:PlayerIndex。它实际上与该类型的不可为null的版本不同

这应该可以做到,您可以查看Nullable是如何工作的


这应该可以做到,您可以查看Nullable是如何工作的

你能用GamePad.GetState()检查控制器是否先插入吗?你能用GamePad.GetState()检查控制器是否先插入吗?C#会抱怨这种语法。。。不能像在C中那样执行隐式空检查,而且仍然存在传递PlayerIndex的问题?对于一个需要PlayerIndex.C的方法,它将抱怨该语法。。。不能像在C中那样执行隐式空检查,而且仍然存在传递PlayerIndex的问题?指向需要PlayerIndex的方法。