未绘制的 Sprite 仍处于活动状态 xna

non drawn Sprite still active xna

我正在制作一个 2D space 射击游戏,我 运行 遇到了盾牌加电的问题。盾牌使玩家在一定时间内对敌舰的碰撞无敌。当玩家与能量提升发生碰撞时,当玩家离开能量提升的位置并且能量提升从屏幕上移除时,玩家在一段时间内处于无敌状态,一切正常。然而,我遇到的问题是,即使在绘制方法中将其从屏幕上移除后,玩家仍然可以与其发生碰撞,这意味着玩家可以简单地留在那个位置并永远立于不败之地。因此,一旦玩家与盾牌发生碰撞,我希望玩家不会再次与它发生碰撞。我试过在线搜索,但没有发现任何特别有用的东西。 我也想再次使用这个电源。将来我计划在我的滚动背景中添加它们 'randomly'。不确定这是否会向 post 添加任何内容,只是想我会包含它。

game1.cs

    public double counterPower = 0;

    public bool powerUpCollision = false;
    public bool invincibility = false;
    Sprite shieldPower;
    bool isVisible = true; 

    protected override void Initialize()
    {
      shieldPower = new Sprite();
    }

    protected override void LoadContent()
    {
        if (isVisible == true)
        {
            shieldPower.LoadContent(this.Content, "powerUpShield");
            shieldPower.Position.X = 300;
            shieldPower.Position.Y = 300;
        }
    }

    protected override void Update(GameTime theGameTime)
    {
        powerUps(theGameTime);
    }

     public void powerUps (GameTime theGameTime)
    {
        if (mPlayer.boundingBox.Intersects(shieldPower.boundingBoxShieldPower)) 
        {
            if (shieldPower.isVisible == true)
            {
                Console.WriteLine("collision working");
                powerUpCollision = true; 
                invincibility = true;
                isVisible = false;
                if (powerUpCollision == true && invincibility == true) 
                {
                    lives = lives - 0; 
                }

                counterPower = 0;
            }
        }
        counterPower += theGameTime.ElapsedGameTime.TotalSeconds;
        {
            Console.WriteLine(counterPower);
            if (counterPower > 7) 
            {
                powerUpCollision = false;
                invincibility = false; 

            }
        }


       protected override void Draw(GameTime theGameTime)
       {
           spriteBatch.Begin();

           if (isVisible == true)
             {
                shieldPower.Draw(this.spriteBatch);
             }
       }
       spriteBatch.End();

在精灵中 class 是加电的边界框,这很好用。 我还有很多代码和 classes,但我很确定其中很多与这个问题无关,我也不想浪费你的时间。

如有任何帮助,我们将不胜感激。 谢谢。

我首先建议的是将边界框设置为 null 或设置为不可能与之发生碰撞的某个位置。听起来玩家飞船正在与物体相撞,因为即使它没有显示,它的所有属性仍然存在于系统中。因此,使它不可见并将其隐藏在屏幕外是一种选择,尽管这是为了在应该调用时使用其他代码设置位置。

第二件事,如果游戏中存在道具,则设置一个标志。基本上将所有 "Do this when the ship hits it" 嵌套在 if 语句中,这样它只会在您需要时应用,并在不满足条件时跳过该代码。如果边界框不会影响其他任何东西,您实际上可以这样做而不是第一部分。

编辑:为了澄清,是这样的:

if(ship.hitsPowerup)
{
   if(powerup.isActive)
   {
       doTheThing;
       powerup.isActive = false;
   }
}