【发布时间】:2016-04-11 10:29:17
【问题描述】:
我的代码中的子弹似乎正在射击入侵者,因为入侵者消失了。然而,没有子弹从坦克中出来并击中入侵者。我不知道子弹去哪儿了。
初始化项目符号代码:
recBullet = new Rectangle();
isRight = true;
isShotFired = false;
isBulletOut = false;
isBulletAlive = true;
更新代码:
KeyboardState keys = Keyboard.GetState();
if ((keys.IsKeyDown(Keys.Space) == true || oldState.IsKeyUp(Keys.Space) == false))
{
recBullet.X = recTank.X + recTank.Width / 2;
recBullet.Y = screenHeight;
isShotFired = true;
isBulletAlive = true;
}if (isShotFired)
{
recBullet.Y -= 10;
}
if (recBullet.Y <= 0)
{
isShotFired = false;
}
for (int x = 0; x < numberOfXInvaders; x++)
{
for (int y = 0; y < numberofYInvaders; y++)
{
if (isBulletAlive)
{
if (recBullet.Intersects(recInvader[x, y]))
{
if (!isInvaderDead[x, y])
{
isInvaderDead[x, y] = true;
isBulletAlive = false;
}
}
}
}
}
oldState = keys;
绘制代码:
if (isBulletAlive)
{
spriteBatch.Draw(texBullet, recBullet, Color.Green);
}
感谢我为此获得的任何帮助!谢谢。
【问题讨论】:
-
确保您已为 textBullet 正确加载资源。您还可以显示调用 spriteBatch.Draw 的方法吗? (如果你没有从覆盖 Draw 中调用它)
-
也有可能正在绘制它,但背景(其他东西)在您的项目符号之上,因此您无法在 UI 上注意到它。
-
另一种可能是子弹移动得太快而看不见。您可以在
recBullet.Y -= 10;语句中尝试较低的值。如果您的更新代码在每次更新时都被调用,那么在 vsync 开启的情况下,它们将以每秒 600 像素的速度移动,如果关闭,则速度会更快。