当一个"敌人"击中一个航点时,所有敌人都会进入队列中的下一个航路点......为什么?

Cou*_*urt 2 c#-4.0

这是我正在研究的代码,我希望所有的敌人能够自己去每个航路点; 然而,当一个敌人击中航路点时,所有敌人都会进入下一个航路点.我如何解决它?

我从main运行它,我有一个敌人类,当我的敌人被创建时,我已经将队列作为参数传递.原始队列称为'wayQ',我的敌人使用的复制队列称为'way'.

编辑:这是敌人类.我修改了代码以覆盖主更新方法.

class Enemy : GameObject
{
    public Texture2D texture;
    public float scale = 0.3f;
    public Queue<Vector2> way = new Queue<Vector2>();
    private int atDestinationLimit = 1;

    public Enemy()
    {
    }

    public Enemy(ContentManager Content, int health, float speed, Vector2 vel, Vector2 pos, Queue<Vector2> wayQ)
    {
        this.Health = health;
        this.Speed = speed;
        this.velocity = vel;
        this.position = pos;
        this.IsAlive = true;
        this.texture = Content.Load <Texture2D>("SquareGuy");
        this.center = new Vector2(((this.texture.Width * this.scale) / 2), ((this.texture.Height * this.scale) / 2));
        this.centPos = this.position + this.center;
        this.way = wayQ;
    }

    public void Update(GameTime theGameTime)
    {
        if (way.Count > 0)
        {
            if (Vector2.Distance(centPos, way.Peek()) < atDestinationLimit)
            {
                float distanceX = MathHelper.Distance(centPos.X, way.Peek().X);
                float distanceY = MathHelper.Distance(centPos.Y, way.Peek().Y);

                centPos = Vector2.Add(centPos, new Vector2(distanceX, distanceY));
                way.Dequeue();
            }
            else
            {
                Vector2 direction = -(centPos - way.Peek());
                direction.Normalize();
                velocity = Vector2.Multiply(direction, Speed);

                centPos += velocity;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 7

修改您的Enemy类以拥有自己的航点列表副本.创建航点列表并将其分配给每个Enemy对象为每个Enemy对象提供对一个列表的引用.当你Dequeue是一个航点时,你可以在一个列表中这样做.