質問

I'm currently working on a xna project, a 2D game. My problem is that when I want the bullet(poop) to appear after I pressed the Up key, i receive the error in the line which is giving the initial position to the Vector2 of my bullet(poop).

// for each bullet in the list, update it
foreach (Poop p in poopList)
{
   p.Update(gameTime);
}

In the Poop class, in the update method, i have a case, depending on the direction in which the bullet should go: Top (1) Right (2) Down (3) Left (4)

case 1:
                position = new Vector2(monitoPosition.X + monitoTexture.Width / 2 -     poopTexture.Width / 2, monitoPosition.Y + monitoTexture.Height / 2);

                position.Y = position.Y - speed;

                    if (position.Y <= 0 || position.Y >=500)
                        isVisible = false;
                    if (position.X <= 0 || position.X >= 800)
                        isVisible = false;

                break;

So in the line, in which I assign a new position to the bullet(poop), it throws the error.

Please Help

役に立ちましたか?

解決

A variable in your code is null, meaning you will get an Object reference not set an instance of an Object error, because it is basically nothing. See this StackOverflow question for more info.

Double check that all your variables are assigned to.

Assuming you are talking about this line:

position = new Vector2(monitoPosition.X + monitoTexture.Width / 2 -     poopTexture.Width / 2, monitoPosition.Y + monitoTexture.Height / 2);

Make sure that monitoPosition and poopTexture are set. You can set a breakpoint on this line to pause the code, and hover over the variables to see if they are null.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top