简体   繁体   English

C#XNA Space Invaders问题

[英]C# XNA Space Invaders issue

I'm making a Space Invaders clone using C# in XNA 4.0 and I've run into a couple of issues. 我正在XNA 4.0中使用C#制作“太空侵略者”克隆,但遇到了两个问题。 The first is that when I shoot all of the invaders on the right-hand side column of the array but the very top one, that invader moves off-screen until the next column reaches the predetermined limit; 第一个是,当我在阵列的右侧列上但在最顶端列上拍摄所有入侵者时,该入侵者会移出屏幕,直到下一列达到预定的限制为止。 then the entire array moves down. 然后整个阵列向下移动。 Obviously I want it to still detect the remaining invader. 显然,我希望它仍然可以检测到剩余的入侵者。 I'm pretty sure the issue is with the following section of code, but I'm not sure what the problem is. 我很确定问题出在以下代码部分,但我不确定问题出在哪里。

    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;
            }
        }

The second issue is that if I destroy all but one row of invaders I get a 'NullReferenceException was unhandled' notification on this piece of code: 第二个问题是,如果我破坏了所有入侵者,但仅破坏了一行入侵者,则会在这段代码上收到“ NullReferenceException was unhanded”通知:

    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);
                }
            }
    }

Again, not sure what the problem is. 同样,不确定是什么问题。 The following is the code for detecting the remaining invader: 以下是用于检测剩余入侵者的代码:

    // 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;
            }
        }

Any help with either issue is greatly appreciated. 任何一个问题的帮助将不胜感激。

So you might be suffering from some off by one errors. 因此,您可能会遇到一些错误。 Looking at your code, it looks like you use a lot of 'magic' numbers everywhere. 查看您的代码,看起来您到处都使用很多“魔术”数字。 The first step would be to remove all the 4s, 10s, 5s, and 11s, and create some public constants: 第一步是删除所有的4、10、5和11,并创建一些公共常量:

static readonly int NumRows = 5;
static readonly int NumColumns = 11;

Now, the first section of your code currently never tests the 0th row or column, which could be leading to some of the badness you're seeing. 现在,您的代码的第一部分目前从未测试过第0行或第0列,这可能会导致您看到一些不良情况。 Your loops could now be written as: 您的循环现在可以写成:

for (int rows = NumRows - 1; rows >= 0; rows--)
    for (int cols = NumCols - 1; cols >= 0; cols--)

Currently, your loop was never testing col or row 0. 目前,您的循环从未测试过col或第0行。

Edit: I missed the 'break' problem. 编辑:我错过了“休息”的问题。 An easy way to solve that is: 一个简单的解决方法是:

bool found = false;
for (int rows = NumRows - 1; rows >= 0 && !found; rows--)
    for (int cols = NumCols - 1; cols >= 0 && !found; cols--)

then set found to true instead of breaking. 然后将found设置为true而不是破坏。 Another way is to place your nested loops inside a function, and return when you find what you want. 另一种方法是将嵌套循环放在函数内,并在找到所需的内容时返回。

For example, your found function could be written: 例如,可以将找到的函数编写为:

for (int rows = 0; rows < NumRows ; rows++)
    for (int cols = 0; cols < NumCols; cols++)
    {
        if (InvaderArray[rows, cols] != null)
        {
            return true;
        }
    }
}
return false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM