无法描述不同类中两个对象之间的碰撞以添加健康栏

时间:2015-12-06 11:47:56

标签: c# object xna collision

我正在尝试为我的2d平台游戏添加一个健康栏。 Atm我试图定义两个对象(火箭和玩家)之间的碰撞,但我不知道如何引用/使用它们。我把错误写成代码中的注释。

这是健康类:

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 Find方法:

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;
}

Rocket类:

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并最终让玩家在没有健康时死亡。

1 个答案:

答案 0 :(得分:0)

在我看来,其中一行或两行

Player player = GameWorld.Find("player") as Player;
Rocket rocket = GameWorld.Find("rocket") as Rocket;

返回null,执行该检查,确保gameObjects列表包含您要查找的内容并且它们是您期望的类型

检查即使你的玩家死了,你在代码部分之前将它从世界中删除它给你的错误,也许在你检查时玩家(或火箭)已经从世界中移除