0

我正在制作一个游戏,其中角色来自屏幕的相对两侧并相互碰撞和攻击,然后在他们死亡时被移除。我已经设法使列表在它们碰撞时停止移动并造成损坏,但我的问题是当它们中的 2 个碰撞时它们都停止移动。我的shortswordsman碰撞代码是:

private void shortMoveCollisions(GameTime gameTime)
    {
        Rectangle shortRect;
        int shortSpeed = 2;
        int shortDamage = 20;
        bool collided = false;

        for (int i = 0; i < shortList.Count; i++)
        {
            List<Goblin> tempGoblinList = new List<Goblin>(goblinList);
            shortRect = new Rectangle((int)shortList[i].position.X, (int)shortList[i].position.Y, ShortSwordsman.texture.Width / 4 - 20, ShortSwordsman.texture.Height);

            foreach (Goblin goblin in tempGoblinList)
            {
                Rectangle goblinRect = new Rectangle((int)goblin.position.X, (int)goblin.position.Y, Goblin.texture.Width / 4 - 20, Goblin.texture.Height);
                if (shortRect.Intersects(goblinRect))
                {
                    collided = true;
                    shortList[i].AnimateAttack(gameTime);

                    shortTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (shortTimer >= shortDelay)
                    {
                        shortTimer -= shortDelay;
                        goblin.health -= shortDamage;
                        if (goblin.health <= 0)
                        {
                            goblinList.Remove(goblin);
                        }
                    }
                }
            }
            if (shortRect.Intersects(background.badCastleRect))
            {
                collided = true;
                shortList[i].AnimateAttack(gameTime);

                shortTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (shortTimer >= shortDelay)
                {
                    shortTimer -= shortDelay;
                    badCastleHealth -= shortDamage;
                }
             }
            if (collided == false)
            {
                shortList[i].AnimateWalk(gameTime);

                shortList[i].position.X += shortSpeed;
            }
        }
    }

我的地精碰撞代码是:

private void GoblinMoveCollisions(GameTime gameTime)
    {
        Rectangle goblinRect;
        int goblinSpeed = 2;
        int goblinDamage = 20;
        bool collided = false;

        for (int i = 0; i < goblinList.Count; i++)
        {
            List<ShortSwordsman> tempShortList = new List<ShortSwordsman>(shortList);
            goblinRect = new Rectangle((int)goblinList[i].position.X, (int)goblinList[i].position.Y, Goblin.texture.Width / 4 - 20, Goblin.texture.Height);

            foreach (ShortSwordsman shortSwordsman in tempShortList)
            {
                Rectangle shortRect = new Rectangle((int)shortSwordsman.position.X, (int)shortSwordsman.position.Y, ShortSwordsman.texture.Width / 4 - 20, ShortSwordsman.texture.Height);
                if (goblinRect.Intersects(shortRect))
                {
                    collided = true;
                    goblinList[i].AnimateAttack(gameTime);

                    goblinAttackTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (goblinAttackTimer >= goblinAttackDelay)
                    {
                        goblinAttackTimer -= goblinAttackDelay;
                        shortSwordsman.health -= goblinDamage;
                        if (shortSwordsman.health <= 0)
                        {
                            shortList.Remove(shortSwordsman);
                        }
                    }
                }
            }
            if (goblinRect.Intersects(background.goodCastleRect))
            {
                collided = true;
                goblinList[i].AnimateAttack(gameTime);

                goblinAttackTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (goblinAttackTimer >= goblinAttackDelay)
                {
                    goblinAttackTimer -= goblinAttackDelay;
                    goodCastleHealth -= goblinDamage;
                }
            }
            if (collided == false)
            {
                goblinList[i].AnimateWalk(gameTime);

                goblinList[i].position.X -= goblinSpeed;
            }
        }
    }
4

2 回答 2

0

您的 collidedbool不应该是这些类的变量,因为这些类似乎在所有实体的整个列表上运行。相反,碰撞检测应该在逐个实体的基础上进行,基本上:使碰撞bool成为地精和剑客的属性。这意味着您还必须检查与其他同类生物的交叉点,可能没有攻击命令。

于 2012-10-15T15:05:27.310 回答
0

这是我目前使用的代码——shortswordsman 和 goblin 是一样的,所以我只展示一个:

在地精类中:

public Goblin(Vector2 position, float health, bool Collided, Rectangle goblinRect)
    {
        this.position = position;
        this.health = health;
        this.Collided = Collided;
        this.goblinRect = goblinRect;
    }

在 game1 类中:

void CreateGoblin()
    {
        goblinList.Add(new Goblin(new Vector2(1900, 270), 100, false, new Rectangle()));
    }

然后对于我尝试过的碰撞 - 这包括尝试阻止它们相互重叠的位:

private void GoblinMoveCollisions(GameTime gameTime)
    {
        int goblinSpeed = 2;
        int goblinDamage = 20;

        for (int i = 0; i < goblinList.Count; i++)
        {
            goblinList[i].Collided = false;
            goblinList[i].goblinRect = new Rectangle((int)goblinList[i].position.X, (int)goblinList[i].position.Y, Goblin.texture.Width / 4 - 20, Goblin.texture.Height);
            List<ShortSwordsman> tempShortList = new List<ShortSwordsman>(shortList);

            foreach (ShortSwordsman shortSwordsman in tempShortList)
            {
                Rectangle shortRect = new Rectangle((int)shortSwordsman.position.X, (int)shortSwordsman.position.Y, ShortSwordsman.texture.Width / 4 - 20, ShortSwordsman.texture.Height);
                if (goblinList[i].goblinRect.Intersects(shortRect))
                {
                    goblinList[i].Collided = true;
                    goblinList[i].AnimateAttack(gameTime);

                    goblinAttackTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (goblinAttackTimer >= goblinAttackDelay)
                    {
                        goblinAttackTimer -= goblinAttackDelay;
                        shortSwordsman.health -= goblinDamage;
                        if (shortSwordsman.health <= 0)
                        {
                            shortList.Remove(shortSwordsman);
                        }
                    }
                }
            }
            if (goblinList[i].goblinRect.Intersects(background.goodCastleRect))
            {
                goblinList[i].Collided = true;
                goblinList[i].AnimateAttack(gameTime);

                goblinAttackTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (goblinAttackTimer >= goblinAttackDelay)
                {
                    goblinAttackTimer -= goblinAttackDelay;
                    goodCastleHealth -= goblinDamage;
                }
            }
            if (goblinList[i].goblinRect.Intersects(goblinList[i].goblinRect))
            {
                goblinList[i].Collided = true;
                goblinList[i].AnimateStill(gameTime);
            }
            if (goblinList[i].Collided == false)
            {
                goblinList[i].AnimateWalk(gameTime);
                goblinList[i].position.X -= goblinSpeed;
            }
        }
    }
于 2012-10-16T15:48:51.743 回答