可以';t描述不同类中的两个对象之间的冲突以添加健康条
本文关键字:对象 两个 之间 冲突 健康 添加 描述 可以 同类 | 更新日期: 2023-09-27 18:25:54
我正试图在我的2d平台上添加一个健康条。我试图定义两个物体(火箭和玩家)之间的碰撞,但我不知道如何引用/使用它们。我已将错误作为注释写入代码中。
这是健康等级:
using Microsoft.Xna.Framework;
class Health : GameObjectList
{
public Health(int layer = 3, string id = "") : base(layer, id)
{
for(int i = 0; i < 255; i+=85)
if(i % 85 == 0)
{
SpriteGameObject health = new SpriteGameObject("Sprites/spr_health", 3);
health.Position = new Vector2(120 + i, 15);
this.Add(health);
}
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
Player player = GameWorld.Find("player") as Player;
Rocket rocket = GameWorld.Find("rocket") as Rocket;
foreach (GameObject obj in gameObjects)
{
SpriteGameObject h = obj as SpriteGameObject;
if (rocket.CollidesWith(player))
//This is wat it says as an error:
//An unhandled exception of type 'System.NullReferenceException' occurred in TickTick.exe
//Additional information: Object reference not set to an instance of an object.
{
this.Remove(h);
return;
}
}
}
}
我在火箭类中使用了相同的GameWorld.find方法,当时没有健康条,并且有效:
public void CheckPlayerCollision()
{
Player player = GameWorld.Find("player") as Player;
if (this.CollidesWith(player) && this.Visible)
player.Die(false);
// CURRENTLY DON'T WANT THIS TO HAPPEN BECAUSE OF HEALTH BAR
}
这是GameWorld的查找方法:
public GameObject Find(string id)
{
foreach (GameObject obj in gameObjects)
{
if (obj.ID == id)
return obj;
if (obj is GameObjectList)
{
GameObjectList objlist = obj as GameObjectList;
GameObject subobj = objlist.Find(id);
if (subobj != null)
return subobj;
}
}
return null;
}
火箭级:
using Microsoft.Xna.Framework;
class Rocket : AnimatedGameObject
{
protected double spawnTime;
protected Vector2 startPosition;
public Rocket(bool moveToLeft, Vector2 startPosition)
{
this.LoadAnimation("Sprites/Rocket/spr_rocket@3", "default", true, 0.2f);
this.PlayAnimation("default");
this.Mirror = moveToLeft;
this.startPosition = startPosition;
Reset();
}
public override void Reset()
{
this.Visible = false;
this.position = startPosition;
this.velocity = Vector2.Zero;
this.spawnTime = GameEnvironment.Random.NextDouble() * 5;
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
if (spawnTime > 0)
{
spawnTime -= gameTime.ElapsedGameTime.TotalSeconds;
return;
}
this.Visible = true;
this.velocity.X = 600;
if (Mirror)
this.velocity.X *= -1f;
CheckPlayerCollision();
// check if we are outside the screen
Rectangle screenBox = new Rectangle(0, 0, GameEnvironment.Screen.X, GameEnvironment.Screen.Y);
if (!screenBox.Intersects(this.BoundingBox))
this.Reset();
}
public void CheckPlayerCollision()
{
Player player = GameWorld.Find("player") as Player;
if (this.CollidesWith(player) && this.Visible)
player.Die(false);
// CURRENTLY DON'T WANT THIS TO HAPPEN BECAUSE OF HEALTH BAR
}
}
我只是不知道我如何才能做到这一点,将生命值降低1,并最终在没有生命值的情况下让玩家死亡。
在我看来,的一行或两行
Player player = GameWorld.Find("player") as Player;
Rocket rocket = GameWorld.Find("rocket") as Rocket;
返回null,进行检查,确保gameObjects列表包含您要查找的内容,并且它们是您期望的类型
检查,即使当你的玩家死亡时,你在它给你错误的代码部分之前将其从世界中删除,也许在你检查时,玩家(或火箭)已经从世界中删除