這裏是我正在研究的代碼,我希望所有的敵人自己去每個航點。然而,當一個敵人擊中航點時,所有敵人都會轉向下一個航點。我如何解決它?當一個'敵人'命中一個航點時,所有敵人都會進入隊列中的下一個航點...爲什麼?
我從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;
}
}
}
}
不,我添加了敵人的班級給你看。 – Court 2010-08-21 21:39:29