我创建了以2生命开头的敌人“蝇”。如果它被子弹击中,我希望特定Fly的运行状况更改为1,下一次更改为0,等等。我知道如何为Fly设置初始运行状况值,但不知道如何在游戏运行时更改它。
我将不胜感激!
public static State RunUpdate(ContentManager content, GameWindow window,
GameTime gameTime)
{
background.Update(window);
player.Update(window, gameTime);
foreach (Enemy e in enemies.ToList())
{
foreach (Bullet b in player.Bullets.ToList())
{
if (e.CheckCollision(b))
{
e.IsAlive = false;
}
}
if (e.IsAlive)
{
if (e.CheckCollision(player))
{
player.IsAlive = false;
}
e.Update(window);
}
else
{
enemies.Remove(e);
}
}
}
public abstract class Enemy : PhysicalObject
{
protected int health;
public Enemy(Texture2D texture, float X, float Y, float speedX,
float speedY, int health) : base(texture, X, Y, 6f, 0.3f)
{
// Without this, the health of the Fly is set to 0 I believe.
// Is there a more correct way to do it?
this.health = health;
}
public abstract void Update(GameWindow window);
}
class Fly : Enemy
{
public Fly(Texture2D texture, float X, float Y) : base(texture, X, Y, 0f, 3f, 2)
{ }
public override void Update(GameWindow window)
{
vector.Y += speed.Y * 7;
if (vector.X > window.ClientBounds.Width - texture.Width || vector.X < 0)
speed.X *= -1;
if (vector.Y > window.ClientBounds.Height - texture.Height || vector.Y < 0)
speed.Y *= -1;
}
}
答案 0 :(得分:1)
您已经具有有效的碰撞检测功能,似乎可以立即击中敌人。尝试将其更改为:
foreach (Bullet b in player.Bullets.ToList())
{
if (e.CheckCollision(b))
{
e.Health--
if(e.Health <= 0) //Health of 0 means dead right?
e.IsAlive = false;
}
}
如果您希望将健康状况保留为受保护的字段,而不是将其公开,则创建一个公共方法来对Enemy进行分类,以将其健康状况降低一个并从该冲突检测块中使用它。