C#奇怪的类行为

时间:2018-11-20 06:44:37

标签: c# unity3d

因此,我正在制作小型团结游戏,并提供了一些与大型nubmer一起使用的课程

这是CurrLevel类的代码

public class CurrLevel {

    public CurrLevel (int levelNum, string id) {
        valuesLevel = "pow" + levelNum.ToString();
        if(levelNum != 0){
            numberFormatSci = "10^" + levelNum.ToString();
        } else {
            numberFormatSci = "";
        }
        identificator = id;
    }
    public string valuesLevel;
    public string numberFormatSci;
    public string identificator;

    public int getValue(){
        return PlayerPrefs.GetInt(identificator+"-"+valuesLevel);
    }

    public void setValue(int value){
        PlayerPrefs.SetInt(identificator+"-"+valuesLevel, value);
    }

    public void add(int value){
        PlayerPrefs.SetInt(identificator+"-"+valuesLevel, PlayerPrefs.GetInt(identificator+"-"+valuesLevel) + value);
    }

    public void substract(int value){
        PlayerPrefs.SetInt(identificator+"-"+valuesLevel, PlayerPrefs.GetInt(identificator+"-"+valuesLevel) - value);
    }
}

这是SomeCurrency类的代码

  public class SomeCurrency {
    public string identificator;
    public  CurrLevel[] levels = new CurrLevel[10];     

    public SomeCurrency(string id){
        identificator = id;
        for(int i = 0; i < 30; i=i+3){
            levels[i/3] = new CurrLevel(i, identificator);
        }
    }

    public void add(int power, double value){
        int full = (int) value;
        int leftover = (int) (value*1000 - full*1000);
        if(power >= 3){
            levels[power/3-1].add(leftover);
        }
        levels[power/3].add(full);
        updateValues();
    }

    public SomeCurrency copy(SomeCurrency CurrToCopy){
        SomeCurrency copy = new SomeCurrency(CurrToCopy.identificator);
        for(int i = 0; i < 30; i++){
            copy.levels[i/3] = CurrToCopy.levels[i/3];
        }
        return copy;
    }
    public void addAnotherCurrency(SomeCurrency anotherCurr){
        for(int i = 0; i < 30; i=i+3){
            this.add(i, anotherCurr.levels[i/3].getValue());
        }
        updateValues();
    }

    public bool substractAnotherCurrency(SomeCurrency anotherCurr){
        SomeCurrency buffer = copy(anotherCurr);
        Debug.Log(anotherCurr.levels[1].getValue());
        if(canSubstract(buffer)){
            Debug.Log(anotherCurr.levels[1].getValue());
            // for(int i = 27; i >= 0; i-=3){
            //  levels[i/3].substract(anotherCurr.levels[i/3].getValue());
            // }
            return true;
        } else {
            return false;
        }
    }

    public bool canSubstract(SomeCurrency fromWhereSubstract){
        bool possible = false;
        for(int i = 0; i < 30; i+=3){
            fromWhereSubstract.levels[i/3].substract(levels[i/3].getValue());
            if(i != 27){
                if(fromWhereSubstract.levels[i/3].getValue() < 0){
                    fromWhereSubstract.levels[i/3+1].substract(1);
                    fromWhereSubstract.levels[i/3].add(1000);
                }
            }
        }
        if(fromWhereSubstract.levels[9].getValue() < 0){
            possible = true;
        }
        return possible;
    }
    public void setValue(int power, double value){
        int full = (int) value;
        int leftover = (int) (value*1000 - full*1000);
        if(power >= 3){
            string beforeid = identificator+"-"+levels[power/3-1].valuesLevel;
            PlayerPrefs.SetInt(beforeid,leftover);
        }
        string thisid = identificator+"-"+levels[power/3].valuesLevel;
        PlayerPrefs.SetInt(thisid,full);
        updateValues();
    }

    public string getStringValue(){
        int maxlvl = 0;
        for(int i = 27; i >= 0; i=i-3){
            if(levels[i/3].getValue() > 0){
                maxlvl = i/3;
                break;
            }
        }
        string result = levels[maxlvl].getValue().ToString();
        if(maxlvl > 0){
            string leftover = levels[maxlvl-1].getValue().ToString();
            while(leftover.Length != 3){
                leftover = "0"+leftover;
            }
            result += "." + leftover + "*" + levels[maxlvl].numberFormatSci;
        }
        return result;
    }

    public void resetValues(){
        for(int i = 0; i < 30; i+=3){
            levels[i/3].setValue(0);
        }
    }

    private void updateValues(){
        for(int i = 0; i < 27; i=i+3){
            levels[i/3] = new CurrLevel(i, identificator);
            if(levels[i/3].getValue() >= 1000){
                levels[i/3].setValue(levels[i/3].getValue()-1000);
                levels[i/3+1].setValue(levels[i/3+1].getValue()+1);
            }
        }
    }
}

所以基本上,我在代码中创建了SomeCurrency的新变量类型

  public NumberFormatting.SomeCurrency playerScore = new NumberFormatting.SomeCurrency("playerScore");
  public NumberFormatting.SomeCurrency playerClickValue = new NumberFormatting.SomeCurrency("playerClickValue");
    playerScore.resetValues();
    playerScore.add(6, 1.32);
    playerClickValue.resetValues();
    playerClickValue.add(3, 103.831);

后来,当玩家单击按钮时,我尝试将一个减去另一个

    Debug.Log(playerClickValue.levels[1].getValue());
    Debug.Log(playerScore.substractAnotherCurrency(playerClickValue));

调试器首先打印103(click函数中playerClickValue.levels [1] .getValue()的原始值),然后再次打印103(从if(canSubstract(buffer)之前的函数substractAnotherCurrency)打印,但打印相同的变量在此canSubstract之后,它将显示783的值。因此,我每次调用substractAnotherCurrency时,我的函数都会以某种方式更改playerClickValue的原始值。 我应该做些什么更改以保持playerClickValue不变,但是仍然可以检查是否可以从另一个SomeCurrency中抽出它,并在检查是否可以之后进行操作。

1 个答案:

答案 0 :(得分:0)

在C#中,对象是通过引用传递的,这意味着,如果您以功能方式修改对象,则该对象将在各处进行修改。您可以阅读有关它的更多信息,这在编码时很重要。看来您尝试在某处进行类似复制的操作,但根本不使用该复制。

还确定要在canSubstact中编辑变量吗?

这个名字暗示了if只会返回一个布尔值并且什么都不会改变,但是您实际上会在其中调用substact

fromWhereSubstract.levels[i/3+1].substract(1);