精灵随机停止绘制XNA 4.0



我正在制作一款简单的2人飞船射击游戏。

当我按住空格键射击时,投射物的精灵就会被绘制出来,但当我松开空格键并再次射击时,第一个精灵根本就没有被绘制出来。

但它仍然在游戏中并按其本意运行,是代码有问题还是有解决方案?

protected override void Draw(GameTime gameTime)
{
    //Loading and Drawing the background through XNA spriteBatch
    spriteBatch.Begin(0, BlendState.AlphaBlend);
    spriteBatch.Draw(backgroundTexture, GraphicsDevice.Viewport.Bounds, Color.White);
    //Drawing the spaceship on the screen
    spriteBatch.Draw(spaceship.sprite, spaceship.position, null, Color.White);
    //Drawing the alienship
    spriteBatch.Draw(alienship.sprite2, alienship.position2, null, Color.White);
    spriteBatch.End();
    //Drawing the projectile
    spriteBatch.Begin(0, BlendState.AlphaBlend);
    if (projectile.alive == true)
    {
        spriteBatch.Draw(projectile.sprite, projectile.position, Color.White);
    }
    //Drawing the 3rd Projectile
    if(projectile3.alive == true)
    {
        spriteBatch.Draw(projectile3.sprite, projectile3.position, Color.Red);
    }
    //Drawing the 4th Projectile
    if (projectile4.alive == true)
    {
        spriteBatch.Draw(projectile4.sprite, projectile4.position, Color.Green);
    }
    //Drawing the Special Projectile
    if (projectile2.alive)
    {
        spriteBatch.Draw(projectile2.sprite, projectile2.position, Color.White);
    }
    spriteBatch.End();
    // TODO: Add your drawing code here
    base.Draw(gameTime);
}

这不是一个真正的答案,但我没有足够的代表来评论。

我不认为这是XNA或任何你发布的代码的问题。相反,问题很可能出现在代码的另一部分。你说精灵没有绘图,基于你例子中的if语句,我将首先检查所有的投射物是否都是活的。

为此,在每个if语句中插入断点,右键单击它们,单击"condition…",然后输入抛射。Isalive == false,看看炮弹是否在不该死的时候死了。

最新更新