【问题标题】:Getting error Field `BaseStats.baseStats.hp' must be fully assigned before control leaves the constructor在控制离开构造函数之前,必须完全分配字段“BaseStats.baseStats.hp”
【发布时间】:2013-04-14 09:40:16
【问题描述】:

我试图弄清楚这一点,但一直没能做到。无法将类型int 隐式转换为BaseStats.Stat

using UnityEngine;
using System.Collections;

public class BaseStats : MonoBehaviour {

    public struct baseStats {
    public string name;
    public int level;
    public Stat hp;
    public int ap;

    public int strength;
    public int toughness;
    public int agility;
    public int intelligence;
    public int willPower;
    public int luck;

    public int attack;
    public int hitPercentage;
    public int defence;
    public int evasionPercentage;
    public int abilityAttack;
    public int abilityDefence;
    public int abilityDefencePercentage;

    public int exp;

        public baseStats(string Name, int Level, int Hp, int Ap, int Strength, int Toughness, int Agility, int Intelligence, int WillPower, int Luck, int Attack, int HitPercentage, int Defence, int EvasionPercentage, int AbilityAttack, int AbilityDefence, int AbilityDefencePercentage, int Exp) {
            name = Name;
            level = Level;
            hp = Hp;
            ap = Ap;

            strength = Strength;
            toughness = Toughness;
            agility = Agility;
            intelligence = Intelligence;
            willPower = WillPower;
            luck = Luck;

            attack = Attack;
            hitPercentage = HitPercentage;
            defence = Defence;
            evasionPercentage = EvasionPercentage;
            abilityAttack = AbilityAttack;
            abilityDefence = AbilityDefence;
            abilityDefencePercentage = AbilityDefencePercentage;

            exp = Exp;

        }
    }

    public class Stat {
        int current;
        int max;
    }

    void Start() {

        baseStats mainChar = new baseStats( 
            "Truth",
            99,
            9999,
            999,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            100,
            255,
            255, 
            100,
            7777777);

        print(mainChar.level);

    }
}

我正在尝试使 HP 达到当前和最大值,这就是教程教我的方式。不幸的是,本教程是在统一脚本中,而我想用 c# 和

进行编码

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    在您的构造函数中分配:

    hp = Hp;
    

    您的参数HP 的类型为int,并且您已将字段hp 定义为Stat

    public Stat hp;
    

    这就是您收到错误的原因,我相信您也想将您的字段定义为int。另外作为旁注,如果您关注.Net naming conventions

    ,它会更好

    【讨论】:

    • @user2279258,或者您可以将字段定义为int 类型,具体取决于您的要求。但是从电话中看来,您正在传递一个整数
    • 如果我将 hp 定义为 int,我将如何将 Stat 传递给 hp 以便 hp 具有当前值和最大值?非常感谢!
    【解决方案2】:

    问题是你通过构造函数传递了一个int (Hp) 并将它分配给一个Stat (hp) 类型的变量:

    hp = Hp;
    

    将构造函数更改为:

    public baseStats(string Name, int Level, Stat Hp /*...*/){
        //...
    }
    

    编辑:

    参数 #3' 不能将 int' 表达式转换为类型 BaseStats.Stat' 和最佳重载方法匹配 BaseStats.baseStats.baseStats(字符串,int,BaseStats.Stat,int,int, int, int, int, int, int, int, int, int, int, int, int, int, int)' 有 一些无效的参数

    您的第二个问题是更改构造函数还必须将正确的对象传递给构造函数:

    baseStats mainChar = new baseStats( 
                "Truth",
                99,
                9999, //It's an int and you have to pass an object of type Stat
                999,
                255,
                255,
                255,
                255,
                255,
                255,
                255,
                255,
                255,
                100,
                255,
                255, 
                100,
                7777777);
    

    改成:

    Stat myStat = new Stat(); //create the object needed
    
    baseStats mainChar = new baseStats( 
                "Truth",
                99,
                myStat,  //pass it!
                999,
                255,
                255,
                255,
                255,
                255,
                255,
                255,
                255,
                255,
                100,
                255,
                255, 
                100,
                7777777);
    

    【讨论】:

    • 好的,我将 int Hp 更改为 Stat Hp,现在得到 Argument #3' cannot convert int' 表达式以键入 BaseStats.Stat' and The best overloaded method match for BaseStats.baseStats.baseStats(string, int, BaseStats.Stat, int, int, int , int, int, int, int, int, int, int, int, int, int, int, int)' 有一些无效参数
    • 谢谢!现在我将如何添加当前和最大 Hp 的实际数量。 ps 摆脱了错误!!!
    • 好的,非常感谢,我想在我解决这个问题之前我需要做更多的学习,我只是不明白,哈哈再次感谢!!!!!!
    • 您必须公开这些字段:public int current; //...。然后在创建对象后为它们分配一个值:myStat.current = 10; //... .
    • 我明白了,为什么 tut 中的人不只是创建一个 hp current 和 hp max 而不是我不明白哈哈,但你帮助了这么多谢谢
    【解决方案3】:

    首先 Stat 应该是 Enum 根据您提供的用法,而不是 class -

    public enum Stat
    {
       int current;
       int max;
    }
    

    这是类型转换的问题。将参数作为 Enum (Stat) 传递 -

    public baseStats(string Name, int Level, Stat Hp, ....)
    

    或在赋值前将其类型转换为枚举(Stat)-

    hp = (Stat)Hp;
    

    【讨论】:

    • 不幸的是,由于Stat 被定义为class 而不是enum,因此无法进行转换。
    • 哦,我明白了。我没有注意到这一点。尽管根据其用法,这应该是枚举。
    • 我看到这么糟糕尝试将类更改为枚举也为了澄清,我遵循一个统一脚本 tut 试图将其转换为 c#,他将他的 Stat 定义为一个类,所以也许我哪里出错了.. . 谢谢大家的帮助!!!
    猜你喜欢
    • 2011-02-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    相关资源
    最近更新 更多