Reputation: 11
I am currently trying to create a clicker/money game and need some help. So basically the app consist of a button which gives +1 coins each time you click it. I would like to change the amount from +1 to +2 if you buy "double coins for 10 coins" as an example.
Click here to see how the app looks right now, it might be easier to understand.
Under is some coding that might be relevant.
@IBAction func button(_ sender: UIButton) {
score += 1
label.text = "Coins: \(score)"
errorLabel.text = ""
func doublee(sender:UIButton) {
score += 2
}
@IBAction func points(_ sender: UIButton) {
if score >= 10 {
score -= 10
label.text = "Coins: \(score)"
doublePoints.isEnabled = false
xLabel.text = "2X"
xLabel.textColor = UIColor.blue
} else {
errorLabel.text = "ERROR, NOT ENOUGH MONEY"
}
Keep in mind that I have just started programming and would appreciate all feedback. Thank you!
Upvotes: 1
Views: 160
Reputation: 673
If I understand your problem correctly, you need some state variable to hold value of how much coins per click you need to add to overall score.
You can do it like this:
class GameViewController: UIViewController {
// this is your state variables
var coinsPerClick = 1
var score = 0
override func viewDidLoad() {
super.viewDidLoad()
label.text = "Coins: \(score)"
errorLabel.text = .empty
}
@IBAction func getCoins(_ sender: UIButton) {
score += coinsPerClick
label.text = "Coins: \(score)"
// you can clear error label here
errorLabel.text = .empty
}
@IBAction func doubleCoinsPerClick(_ sender: UIButton) {
guard canUpgrade() else {
errorLabel.text = "ERROR, NOT ENOUGH MONEY"
return
}
doublePoints.isEnabled = false
score -= 10
coinsPerClick *= 2
label.text = "Coins: \(score)"
}
private func canUpgrade() -> Bool {
return score >= 10 && doublePoints.isEnabled
}
}
Few remarks on your code:
I hope it will help. Feel free to ask more questions
Upvotes: 0
Reputation: 81
Add a variable which keeps track of how many points you get when you tap the button, and when you buy the score multipliers, increase the variable accordingly, like so:
var scoreIncrease = 1 // this is how much the score increases when you tap
// This is called when the "CLICK HERE" button is tapped
@IBAction func button(_ sender: UIButton) {
score += scoreIncrease
label.text = "Coins: \(score)"
errorLabel.text = ""
}
// This is called when you buy the 2x
@IBAction func points(_ sender: UIButton) {
if score >= 10 {
score -= 10
label.text = "Coins: \(score)"
doublePoints.isEnabled = false
xLabel.text = "2X"
xLabel.textColor = UIColor.blue
scoreIncrease *= 2 // increase by x2
} else {
errorLabel.text = "ERROR, NOT ENOUGH MONEY"
}
}
Upvotes: 1