【问题标题】:Operator `*' cannot be applied to operands of type `method group' and `float'运算符“*”不能应用于“方法组”和“浮点”类型的操作数
【发布时间】:2017-03-01 08:47:25
【问题描述】:

我正在关注关于如何创建 Hack'N'Slash 游戏的 BurgZergArcade 教程。在此期间我遇到了一个错误,如果有人可以帮助我,我将非常感激。语言是 c#。

错误:Assets/Scripts/Character Classes/ModifiedStat.cs(21,66):错误 CS0019:运算符 *' cannot be applied to operands of typemethod group' 和 `float'

public class BaseStat {
    public class BaseStat {
    private int _baseValue;         //The base value of this stat
    private int _buffValue;         //The amount of buff to this stat
    private int _expToLevel;        //Thetotal amount of exp needed to raise this skill
    private float _levelModifier;   //The modifier applied to the exp needed to raise the skill

    public BaseStat() {
        _baseValue = 0;
        _buffValue = 0;
        _levelModifier = 1.1f;
        _expToLevel = 100;
    }

    public int BaseValue{
        get{ return _baseValue; }
        set{ _baseValue = value; }
    }
    public int BuffValue{
        get{ return _buffValue; }
        set{ _buffValue = value; }
    }
    public int ExpToLevel{
        get{ return _expToLevel; }
        set{ _expToLevel = value; }
    }
    public float LevelModifier{
        get{ return _levelModifier; }
        set{ _levelModifier = value; }
    }

    private int CalculateExpToLevel() {
        return (int)(_expToLevel * _levelModifier);
    }
    public void LevelUp() {
        _expToLevel = CalculateExpToLevel();
        _baseValue++;
    }
    public int AdjustedValue() {
        return _baseValue + _buffValue;
    }
}

public class Atrribute : BaseStat {
    public Atrribute() {
        ExpToLevel = 50;
        LevelModifier = 1.05f;
    }
}

public enum AttributeName{
Might,
Constitution,
Nimbleness,
Speed,
Concentration,
Willpower,
Charisma
}

public class ModifiedStat : BaseStat {
    private List<ModifyingAttribute> _mods;         //A list of Attributes that modify this stat
    private int _modValue;                          //

    public ModifiedStat(){
        _mods = new List<ModifyingAttribute>();
        _modValue = 0;
    }

    public void AddModifier( ModifyingAttribute mod ) {
        _mods.Add(mod);
    }

    private void CalculateModValue() {
        _modValue = 0;

        if(_mods.Count > 0)
            foreach( ModifyingAttribute att in _mods )
                _modValue += (int)(att.attribute.AdjustedValue * att.ratio);
    }
}

public struct ModifyingAttribute {
    public Atrribute attribute;
    public float ratio;
}

【问题讨论】:

  • 类似的消息几乎总是表明您在调用方法名称时忘记了方法名称后面的括号。
  • 顺便说一句,考虑使用隐式实现的属性和支持字段,因为这将显着清理您的代码

标签: c# unity3d


【解决方案1】:

由于BaseState.AdjustedValue 是一种方法,我会将ModifiedStat.CalculateModValue() 中的行更改为:

_modValue += (int)(att.attribute.AdjustedValue() * att.ratio);

请注意AdjustedValue后面的()

【讨论】:

  • @Maarten 我在这行代码中遇到了同样的错误bPointP0 = points [2] + (Vector3.Lerp (points [0], reflectedCp1, t) - points [2]).Normalize * (points [0] - points [2]).magnitude;
猜你喜欢
  • 1970-01-01
  • 2013-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
相关资源
最近更新 更多