vendredi 11 novembre 2016

Simple if-clause not working, can't find the mistake

So I am working on an incremental game at the moment. In case you don't know what this is a short explanation. You click a button to get some kind of money (in my case gold). With enough money you can buy stuff that makes you money without even clicking. With more money you can get better upgrades which makes you even more money and so on.

Now to my problem; I created a class called Upgrade which you can see here:

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;
    }
}

The variables should explain themselves.

Now I created some Upgrades via the constructor:

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 

To buy upgrades I have written the method buyUpgrade which is bound to buttons referring to the upgrades listed above.

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));
    }
}

And here comes the problem. As the game starts you have 0 gold. That 0 is stored in the variable goldCount. But even though goldCount is I can buy every Upgrade in unlimited amounts. I just cant figure out the problem. Probably it is really simple and afterwards I realize how stupid I was but I just cant figure it out.

Every help is appreciated. Thank you!

Aucun commentaire:

Enregistrer un commentaire