2016-11-11 21 views
-1

所以我現在正在做一個增量遊戲。如果你不知道這是一個簡短的解釋。你點擊一個按鈕來獲得某種金錢(在我的情況下是黃金)。有了足夠的錢,你可以購買無需點擊即可賺錢的東西。有了更多的錢,你可以得到更好的升級,這使得你更多的錢等等。簡單的if-clause不起作用,找不到錯誤

現在我的問題;我創建了一個名爲Upgrade,你可以在這裏看到類:

public class Upgrade { 
    double baseCost; 
    double baseIncome; 
    int count =0; 
    double cost = baseCost*Math.pow(1.07,count); 
    double goldPerSecond = baseIncome; //TODO find usefull formula! 

    public Upgrade(double baseC, double baseIn){ 
     baseCost = baseC; 
     baseIncome = baseIn; 
    } 
} 

的變量應該解釋自己。

現在我通過構造函數創建了一些Upgrade S:

Upgrade waitress = new Upgrade(50 ,2); //Constructor for Waitress 
Upgrade seats = new Upgrade(100, 5); //Constructor for More-Seats 
Upgrade decoration = new Upgrade(500, 20); //Constructor for Decoration 
Upgrade bartender = new Upgrade (1000, 50); //Constructor for Bartender 

購買升級我寫這勢必按鈕參照上面列出的升級方法buyUpgrade

public void buyUpgrade(Upgrade U) { 
    if (goldCount >= U.cost) { 
     goldCount = goldCount - U.cost; 
     clickGoldPerSecond++; 
     U.count++; 
    } else { 
     error.setText(getResources().getString(R.string.no_gold)); 
    } 
} 

問題出在這裏。隨着比賽開始,你有0金。 0存儲在變量goldCount中。但即使goldCount是我可以無限量購買每Upgrade。我只是不知道這個問題。也許這很簡單,之後我意識到我有多愚蠢,但我無法弄清楚。

每一個幫助表示讚賞。謝謝!

+1

想想什麼樣的價值'baseCost'當你確定升級的'cost'了。 – molbdnilo

回答

0

這裏是你的問題:

int count =0; 
double cost = baseCost*Math.pow(1.07,count); // cost is set and will not be updated 

請注意,您在構造函數更新baseCost,但默認值0.0被用於baseCost * Math.pow(...);之前。但即使你更新了它,通過在構造函數中完成它,仍然不會看到任何價格上漲,因爲成本沒有被重新計算。

  • 在構造函數中設置初始值(或者在訪問成本時總是將baseCost添加到成本中)。
  • 製作一個方法,將count加1,然後Math.pow(...)再加上cost

現在有一個更好的解決方案:因此,在這種情況下,屬性成本是無用的。改變成一個方法:

public double getCost() { 
    return baseCost*Math.pow(1.07,count); 
} 

調用它(其他城市2X U.cost - >U.getCost())和你的天空是藍色一遍,沒有進一步的變化需要的地方永遠。

0

你錯過了getter/setter的成本我猜。因此它總是返回0.0。

我的意思是嘗試用構造函數創建升級對象,然後打印出成本。不管你在構造函數中給出了什麼,對象的成本總是打印出「0.0」。

+0

我更新了我的答案:)但你仍然建議做一個方法,增加一個數:)這是不必要的。公式沒有錯,他只是沒有設置成本,應用程序每次獲得默認值0.0。 –

0

問題是

double baseCost; 
double baseIncome; 
int count =0; 
double cost = baseCost*Math.pow(1.07,count); 

baseCost是所述類的成員變量,被初始化爲0.0 SO「成本」設定爲0.0之後,它從不改變。

您應該在構造函數中移動成本計算。

public Upgrade(double baseC, double baseIn){ 
    baseCost = baseC; 
    baseIncome = baseIn; 
    cost = baseCost*Math.pow(1.07,count); 
} 
+0

儘管如此,它仍然適用於第一個,但成本永遠不會更新 –

+0

成本應該只更新一次。 goldCount每次都會減少成本。 – AbhijitG

+0

baseCost設置爲零...零*任何數字爲零:-) – AbhijitG

0
public class Upgrade 
{ 
double baseCost; 
double baseIncome; 
int count =0; 
double cost ; 
double goldPerSecond; 

public Upgrade(double baseC, double baseIn) 
{ 
    baseCost = baseC; 
    cost = baseCost * Math.Pow(1.07,count); 
    baseIncome = baseIn; 
} 
} 

Your Upgrade class should be similar to what I have created here. If there is a non static filed in the class and you are assigning value to that variable in the way you were doing it will not work. 

In your case you also cannot use the static variable as the variable baseCost is passed as constructor parameter and will not be available until you create an instance of upgrade class. 

If Condition if(goldCount >= U.cost){} Here the cost will have value of zero only as it would not get updated. 
+0

儘管如此,它仍然是第一次,但成本永遠不會得到更新 –