所以我现在正在进行增量游戏。如果您不知道这是一个简短的解释。你点击一个按钮获得某种金钱(在我的情况下是黄金)。有足够的钱,你可以购买的东西,甚至没有点击,让你赚钱。有了更多的钱,你可以获得更好的升级,这会让你获得更多的钱等等。
现在我的问题;我创建了一个名为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
:
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
购买一次。我只是无法弄清楚问题所在。可能它真的很简单,之后我意识到我是多么愚蠢,但我无法弄明白。
感谢每一位帮助。谢谢!
答案 0 :(得分: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
上添加一个,然后再次在Math.pow(...)
上cost
。现在有一个更好的解决方案:因此,在这种情况下,属性成本是无用的。将其更改为方法:
public double getCost() {
return baseCost*Math.pow(1.07,count);
}
打电话给它(更改2x U.cost
- > U.getCost()
),您的天空将再次变为蓝色,无论如何都不需要进一步更改。
答案 1 :(得分:0)
我猜你错过了getter / setter的成本。因此它总是返回0.0。
我的意思是尝试使用构造函数创建一个Upgrade对象,然后打印出成本。无论你在构造函数中给出什么,对象的成本总是打印出来" 0.0"。
答案 2 :(得分:0)
问题在于
double baseCost;
double baseIncome;
int count =0;
double cost = baseCost*Math.pow(1.07,count);
baseCost是该类的成员变量,初始化为0.0 所以"成本"设置为0.0,之后它永远不会改变。
您应该在构造函数中移动成本计算。
public Upgrade(double baseC, double baseIn){
baseCost = baseC;
baseIncome = baseIn;
cost = baseCost*Math.pow(1.07,count);
}
答案 3 :(得分: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.