尝试确定健康栏的另一个类中的两个对象之间的碰撞

时间:2015-12-05 14:39:11

标签: c# xna

这是健康类:

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);
    CheckAllPlayerCollisions();
}

public void CheckAllPlayerCollisions()
{
    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 (player.CollidesWith(rocket))
        {
            this.Remove(h);
            return;
        }
    }
}
}

'if(player.CollidesWith(rocket))'行给出了这个错误:

TickTick.exe中发生未处理的“System.NullReferenceException”类型异常

附加信息:未将对象引用设置为对象的实例。

这是火箭类:

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

这是球员级别:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;

partial class Player : AnimatedGameObject
{
protected Vector2 startPosition;
protected bool isOnTheGround;
protected float previousYPosition;
protected bool isAlive;
protected bool exploded;
protected bool finished;
protected bool walkingOnIce, walkingOnHot;

public Player(Vector2 start) : base(2, "player")
{
    this.LoadAnimation("Sprites/Player/spr_idle", "idle", true); 
    this.LoadAnimation("Sprites/Player/spr_run@13", "run", true, 0.05f);
    this.LoadAnimation("Sprites/Player/spr_jump@14", "jump", false, 0.05f); 
    this.LoadAnimation("Sprites/Player/spr_celebrate@14", "celebrate", false, 0.05f);
    this.LoadAnimation("Sprites/Player/spr_die@5", "die", false);
    this.LoadAnimation("Sprites/Player/spr_explode@5x5", "explode", false, 0.04f); 

    startPosition = start;
    Reset();
}

public override void Reset()
{
    this.position = startPosition;
    this.velocity = Vector2.Zero;
    isOnTheGround = true;
    isAlive = true;
    exploded = false;
    finished = false;
    walkingOnIce = false;
    walkingOnHot = false;
    this.PlayAnimation("idle");
    previousYPosition = BoundingBox.Bottom;
}

public override void HandleInput(InputHelper inputHelper)
{
    float walkingSpeed = 400;
    if (walkingOnIce)
        walkingSpeed *= 1.5f;
    if (!isAlive)
        return;
    if (inputHelper.IsKeyDown(Keys.Left))
        velocity.X = -walkingSpeed;
    else if (inputHelper.IsKeyDown(Keys.Right))
        velocity.X = walkingSpeed;
    else if (!walkingOnIce && isOnTheGround)
        velocity.X = 0.0f;
    if (velocity.X != 0.0f)
        Mirror = velocity.X < 0;
    if ((inputHelper.KeyPressed(Keys.Space) || inputHelper.KeyPressed(Keys.Up)) && isOnTheGround)
        Jump();
}

public override void Update(GameTime gameTime)
{
    base.Update(gameTime);
    if (!finished && isAlive)
    {
        if (isOnTheGround)
            if (velocity.X == 0)
                this.PlayAnimation("idle");
            else
                this.PlayAnimation("run");
        else if (velocity.Y < 0)
            this.PlayAnimation("jump");

        TimerGameObject timer = GameWorld.Find("timer") as TimerGameObject;
        if (walkingOnHot)
            timer.Multiplier = 2;
        else if (walkingOnIce)
            timer.Multiplier = 0.5;
        else
            timer.Multiplier = 1;

        TileField tiles = GameWorld.Find("tiles") as TileField;
        if (BoundingBox.Top >= tiles.Rows * tiles.CellHeight)
            this.Die(true);
    }

    DoPhysics();
}

public void Explode()
{
    if (!isAlive || finished)
        return;
    isAlive = false;
    exploded = true;
    velocity = Vector2.Zero;
    position.Y += 15;
    this.PlayAnimation("explode");
}

public void Die(bool falling)
{
    if (!isAlive || finished)
        return;
    isAlive = false;
    velocity.X = 0.0f;
    if (falling)
        GameEnvironment.AssetManager.PlaySound("Sounds/snd_player_fall");
    else
    {
        velocity.Y = -900;
        GameEnvironment.AssetManager.PlaySound("Sounds/snd_player_die");
    }
    this.PlayAnimation("die");
}

public bool IsAlive
{
    get { return isAlive; }
}

public bool Finished
{
    get { return finished; }
}

public void LevelFinished()
{
    finished = true;
    velocity.X = 0.0f;
    this.PlayAnimation("celebrate");
    GameEnvironment.AssetManager.PlaySound("Sounds/snd_player_won");
}
}

我无法弄清楚为什么它不起作用..

0 个答案:

没有答案