【发布时间】:2016-07-02 20:57:15
【问题描述】:
我正在使用最新版本的 unity,并且我有一个属性为 public AllStats BaseStats { get; set; } 的类,其中 AllStats 类的实现如下:
public class AllStats : IEnumerable<Stats>
{
private MagicPower magicPower;
public MagicPower MagicPower
{
get { return magicPower; }
set
{
magicPower = value;
Add("Magic Power", magicPower);
}
}
private DebuffDuration debuffDuration;
public DebuffDuration DebuffDuration
{
get { return debuffDuration; }
set
{
debuffDuration = value;
Add("Debuff Duration", debuffDuration);
}
}
private Defense defense;
public Defense Defense
{
get { return defense; }
set
{
defense = value;
Add("Defense", defense);
}
}
private MagicDefense magicDefense;
public MagicDefense MagicDefense
{
get { return magicDefense; }
set
{
magicDefense = value;
Add("Magic Defense", magicDefense);
}
}
private CriticalPower criticalPower;
public CriticalPower CriticalPower
{
get { return criticalPower; }
set
{
criticalPower = value;
Add("Critical Power", criticalPower);
}
}
private CriticalChance criticalChance;
public CriticalChance CriticalChance
{
get { return criticalChance; }
set
{
criticalChance = value;
Add("Critical Chance", criticalChance);
}
}
private Multistrike multistrike;
public Multistrike Multistrike
{
get { return multistrike; }
set
{
multistrike = value;
Add("Multistrike", multistrike);
}
}
private CooldownReduction cooldownReduction;
public CooldownReduction CooldownReduction
{
get { return cooldownReduction; }
set
{
cooldownReduction = value;
Add("Cooldown Reduction", cooldownReduction);
}
}
private Evasion evasion;
public Evasion Evasion
{
get { return evasion; }
set
{
evasion = value;
Add("Evasion", evasion);
}
}
private Resistance resistance;
public Resistance Resistance
{
get { return resistance; }
set
{
resistance = value;
Add("Resistance", resistance);
}
}
private readonly Dictionary<string, Stats> _items = new Dictionary<string, Stats>();
public void Add(string element, Stats config)
{
_items[element] = config;
}
public Stats this[string element]
{
get { return _items.ContainsKey(element) ? _items[element] : null; }
set { _items[element] = value; }
}
public IEnumerator<Stats> GetEnumerator()
{
return _items.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _items.Values.GetEnumerator();
}
}
但是,当我尝试像这样创建此类的新实例时:
BaseStats = new AllStats()
{
MagicPower = new MagicPower(Level.CurrentLevel),
DebuffDuration = new DebuffDuration(Level.CurrentLevel),
Defense = new Defense(Level.CurrentLevel),
MagicDefense = new MagicDefense(Level.CurrentLevel),
CriticalPower = new CriticalPower(1),
CriticalChance = new CriticalChance(1),
Multistrike = new Multistrike(1),
CooldownReduction = new CooldownReduction(1),
Evasion = new Evasion(1),
Resistance = new Resistance(1)
};
我得到空引用异常。这是为什么 ?这曾经在普通的视觉工作室中工作,但似乎旧版本的统一导致了一些问题。我该如何解决这个问题?
这是Stats 类:
public enum StatsOrders
{
MagicPower,
DebuffDuration,
Defense,
MagicDefense,
CriticalPower,
CriticalChance,
Multistrike,
CooldownReduction,
Evasion,
Resistance
}
public abstract class Stats : IStats
{
public double PointsInStats { get; set; }
public bool IsSuccessfulHit(double baseStats)
{
return Increase(baseStats)*10 >= new RngCrypto().Next(1, 1001);
}
public double Increase(double baseInput)
{
return CalculateIncreament(baseInput, PointsInStats);
}
protected abstract double percentageIncrease { get; set; }
protected Stats(double pointsInStats)
{
PointsInStats = pointsInStats;
}
protected double CalculateIncreament(double baseInput, double pointsInStats)
{
string a = (baseInput * (pointsInStats / percentageIncrease)).ToString("N1");
return double.Parse(a);
}
}
当您升级时会使用该枚举,您会收到一个包含更新统计信息的小通知窗口,此枚举有助于保持它们的顺序。
还有IStats 接口:
public interface IStats
{
double PointsInStats { get; set; }
bool IsSuccessfulHit(double baseStats);
double Increase(double input);
}
【问题讨论】:
-
你在哪里初始化了_items?我认为因为它可能会给出一个空引用异常
-
@Mysterio11 在字段初始化器中
-
因为 AllStats 继承了 IEnumerable
形式,请尝试替换为 AllStats : Stats -
所有关于游戏到底是什么的问题/cmets都无关紧要,请专注于实际问题,因为评论线程变得非常嘈杂。
-
除非大体上我们知道游戏中到底发生了什么,否则不可能知道问题可能出在哪里。不管怎样,我现在要走了,再见!