【问题标题】:Collision detection with a List使用列表进行碰撞检测
【发布时间】:2014-12-03 15:32:33
【问题描述】:

我得到了一个名为对象的敌人列表和一个子弹列表。

private List<enemyObjects> objects = new List<enemyObjects>();

list 对象在 Game1 类中。

我用这个方法在列表中添加和删除敌人:

public void LoadEnemies() {
        int Y = 100;
        if (spawn >= 1) 
        {
            spawn = 0;
            if (objects.Count() < 4)
                objects.Add(new enemyObjects(Content.Load<Texture2D>("obstacle"), new Vector2(1100, Y)));
        }

        for (int i = 0; i < objects.Count; i++)
        {
            if (!objects[i].isVisible)
            {
                //If the enemy is out of the screen delete it
                objects.RemoveAt(i);
                i--;
            }
        }
    }

我还得到了一个子弹列表:public List<Bullet> bullets = new List<Bullet>(); 我发射子弹的方法:

private void ShootFireBall() {
        if (mCurrentState == State.Walking)
        {
            bool aCreateNew = true;
            foreach (Bullet aBullet in bullets)
            {
                if (aBullet.bulletVisible == false)
                {
                    aCreateNew = false;
                    aBullet.Fire(position + new Vector2(Size.Width / 2, Size.Height / 2),
                        new Vector2(200, 0), new Vector2(1, 0));
                }
            }

            if (aCreateNew)
            {
                Bullet aBullet = new Bullet();
                aBullet.LoadContent(contentManager, "bullet");
                aBullet.Fire(position + new Vector2(Size.Width / 2, Size.Height / 2),
                       new Vector2(200, 0), new Vector2(1, 0));
                bullets.Add(aBullet);
            }
        }
    }

问题是我需要一个矩形,所以我可以检查是否有碰撞。 如何检查与 2 个列表的冲突? 有什么办法可以将其转换为矩形? 卡在这个问题上几个小时了,真的想不通。

【问题讨论】:

    标签: c# xna


    【解决方案1】:

    我的所有精灵通常都派生自一个通用类型。 Sprite、GameEntity 等等。该基本类型将公开诸如BoundsLocation 等属性。

    类似:

    public abstract class Sprite
    {
        public Vector2 Location { get; set; }
        public Rectangle Bounds
        {
            get
            {
                return new Rectangle((int)Location.X, (int)Location.Y, 
                                     _texture.Width, _texture.Height);
            }
        }
    
        private Texture2D _texture;
    
        public Sprite(Texute2D texture)
        {
            _texture = texture;
        }
    }
    
    public class enemyObjects : Sprite
    {
    
        // enemy-specific properties go here
    
        public enemyObjects(Texture2D texture)
            : base(texture)
        {
        }
    }
    
    public class Bullet : Sprite
    {
    
        // Bullet-specific properties go here
    
        public Bullet(Texture2D texture)
            : base(texture)
        {
        }
    }
    

    然后您可以简单地使用objects[i].Bounds 来获取包含该对象的矩形。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      相关资源
      最近更新 更多