Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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#XNA太空入侵者问题_C#_Xna - Fatal编程技术网

C#XNA太空入侵者问题

C#XNA太空入侵者问题,c#,xna,C#,Xna,我正在使用XNA4.0中的C#制作一个太空入侵者克隆,我遇到了几个问题。第一种情况是,当我射杀阵列右侧列中除最上面一列之外的所有入侵者时,入侵者会离开屏幕,直到下一列达到预定的限制;然后整个阵列向下移动。显然,我希望它仍然能检测到剩余的入侵者。我很确定问题出在下面的代码部分,但我不确定问题出在哪里 for (int rows = 4; rows > 0; rows--) // Detects right-most invader for (int cols = 10

我正在使用XNA4.0中的C#制作一个太空入侵者克隆,我遇到了几个问题。第一种情况是,当我射杀阵列右侧列中除最上面一列之外的所有入侵者时,入侵者会离开屏幕,直到下一列达到预定的限制;然后整个阵列向下移动。显然,我希望它仍然能检测到剩余的入侵者。我很确定问题出在下面的代码部分,但我不确定问题出在哪里

    for (int rows = 4; rows > 0; rows--) // Detects right-most invader
        for (int cols = 10; cols > 0; cols--)
        {
            if (InvaderArray[rows, cols] != null)
            {
                RightInvader = InvaderArray[rows, cols];
                break;
            }
        }
第二个问题是,如果我销毁了除一行入侵者之外的所有入侵者,我会收到关于这段代码的“NullReferenceException was unhandled”通知:

    if (RightInvader.GetXPos() > 800) // Right edge limit
    {
        InvaderDir = -1;

        for (int rows = 0; rows < 5; rows++)
            for (int cols = 0; cols < 11; cols++)
            {
                if (InvaderArray[rows, cols] != null) 
                {
                    InvaderArray[rows, cols].MoveVertical(8);
                }
            }
    }
if(righinvader.GetXPos()>800)//右边缘限制
{
入侵者受体=-1;
对于(int行=0;行<5;行++)
for(int cols=0;cols<11;cols++)
{
if(rarray[行,列]!=null)
{
数组[行,列].MoveVertical(8);
}
}
}
同样,我也不确定问题出在哪里。以下是检测剩余入侵者的代码:

    // Detecting remaining invader
    bool InvaderFound = false;

    for (int rows = 0; rows < 5; rows++)
        for (int cols = 0; cols < 11; cols++)
        {
            if (InvaderArray[rows, cols] != null)
            {
                InvaderFound = true;
                break;
            }
        }
//检测剩余入侵者
bool investerfound=假;
对于(int行=0;行<5;行++)
for(int cols=0;cols<11;cols++)
{
if(rarray[行,列]!=null)
{
InvesterFound=true;
打破
}
}

非常感谢您在这两个问题上提供的任何帮助。

因此,您可能会遇到一些“一个接一个”的错误。看看你的代码,看起来你在任何地方都使用了很多“神奇”的数字。第一步是删除所有4s、10s、5s和11s,并创建一些公共常量:

static readonly int NumRows = 5;
static readonly int NumColumns = 11;
现在,代码的第一部分目前从未测试第0行或第0列,这可能导致您看到的一些不好的地方。 您的循环现在可以写成:

for (int rows = NumRows - 1; rows >= 0; rows--)
    for (int cols = NumCols - 1; cols >= 0; cols--)
当前,您的循环从未测试列或行0

编辑: 我错过了“休息”的问题。解决这一问题的简单方法是:

bool found = false;
for (int rows = NumRows - 1; rows >= 0 && !found; rows--)
    for (int cols = NumCols - 1; cols >= 0 && !found; cols--)
然后将find设置为true,而不是break。 另一种方法是将嵌套循环放置在函数中,并在找到所需内容时返回

例如,您找到的函数可以编写为:

for (int rows = 0; rows < NumRows ; rows++)
    for (int cols = 0; cols < NumCols; cols++)
    {
        if (InvaderArray[rows, cols] != null)
        {
            return true;
        }
    }
}
return false;
for(int rows=0;rows
您的第一个循环没有在0索引中迭代,这是预期的行为吗?不应该是rows>=0和cols>=0?而且,
break
语句只会中断内部循环。外部循环继续,这可能不是您想要的。引入一个变量来决定是否继续外部循环<如果找不到合适的元素,code>RightInvader可能为
null
。在访问它的
GetXPos()
之前,您应该检查一下。您可能需要发布代码,以便在“杀死”入侵者时修改入侵者射线。您是否将其设置为null或其他值?因此,对于第一位,在
rightInvester=investraray[rows,cols]之后,我应该在哪里将find设置为true?在之前或之后。下次循环时!found将为false,它将从这两个for循环中退出。当然,如果你把整个东西嵌套在一个名为GetRightInvader的函数中,你可以在那一点上返回InvaderArray[row,col]。好的,我在我的左手和右手入侵者检测器中都添加了它,但是我现在在设置左右边缘限制的代码上得到了一个“NullReferenceExeption was unhandled”(NullReferenceExption was unhandled),我也需要对它们做些什么吗?这里有问题的代码:您的问题是这里的这一行:
if(RightInvader.GetXPos()>800)
。如果RightInvader为空会发生什么,它可以是空的。