非绘制雪碧仍然有效

本文关键字:有效 绘制 | 更新日期: 2023-09-27 18:09:17

我正在制作一款2D太空射击游戏,我遇到了一个关于盾牌能量提升的问题。这个盾牌可以让玩家在一定时间内不受敌舰的攻击。当玩家与升级道具发生碰撞时,当玩家离开升级道具的位置,升级道具从屏幕上移除时,玩家在一段时间内是无敌的。然而,我遇到的问题是,玩家仍然可以与它碰撞,即使它在绘制方法中从屏幕中移除,这意味着玩家可以简单地停留在那个位置,永远是无敌的。所以一旦玩家与盾牌相撞,我希望玩家不会再与之相撞。我试着在网上搜索,但没有找到任何特别有用的东西。我也想再次使用这个能力。在未来,我计划在我的滚动背景中"随机"添加它们。不确定这是否对帖子有什么帮助,只是想我应该把它包括进去。

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

在精灵类中是用于能量提升的边界框,这很有效。我有更多的代码和类,但我相当确定其中很多与这个问题无关,我也不想浪费你的时间。

任何帮助都将非常感激。谢谢你。

非绘制雪碧仍然有效

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

第二件事,设置一个标志,说明游戏中是否存在升级道具。基本上,在if语句中嵌套所有"当船撞到它时执行此操作",这样它只会在你想要的时候应用,并且在不满足条件时跳过该代码。如果边界框不影响其他内容,你可以用这个来代替第一部分。

编辑:澄清一下,像这样:

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