初学者:“在RPG.Core.dll中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理”

时间:2015-04-14 16:29:21

标签: c# nullreferenceexception

这可能是相当微不足道的事情,但我已经决定先跳到这里,并且不太了解如何处理这个问题。

   private void OnBattleUpdate(object sender, ElapsedEventArgs e)
    {
        if (monster.IsAlive)
        {
            ActionResult result = monster.GetAttackResult();
            messages.Add(result.LookMessage);
            state.Character.CauseDamage(result.HealthChange);
            if (state.Character.Health <= 0)
            {
                messages.Add("You are dead.");
            }

            int playerDamage = state.Character.GetDamage().HealthChange;
            messages.Add(String.Format("You swing your sword at {0} and cause {1} damage", monster.Name, playerDamage));
            monster.CauseDamage(playerDamage);
            messages.Add(String.Format("Health : {0}/{1}", state.Character.Health, state.Character.MaxHealth));
        }
        else
        {
            messages.Add(String.Format("{0} is dead.", monster.Name));
            state.Character.Gold += monster.Gold;
            mapManager.RemoveMonster(monster, mapManager.MapName);
            battleTimer.Stop();
        }
    }

处理&#34; IsAlive&#34;

的代码
    private bool _isAlive;
    public bool IsAlive
    {
        get { return _isAlive; }
        set
        {
            if (Health > 0)
            {
                _isAlive = true;
            }
            else
            {
                _isAlive = false;
            }
        }
    }

我只是以错误的方式接近这个吗?你们都能提供的任何提示都会非常有用:)

0 个答案:

没有答案