Need help making Swift Cookie Clicker Game Automatically Save

264 views Asked by At

I am a 9th grader just learning programming. After I got the basics, I figured I'd make a cookie clicker game in swift. I accomplished that and am still working on it to refine it and make it more playable, but I cannot get it to automatically save. I have tried some other lines pasted in and modified but I am doing something wrong. I want it to save the variable click and mult automatically when updated. Any help is appreciated, thanks.

//
//  ViewController.swift
//  EthanClicker
//
//  Created by Ethan on 10/4/20.
//
import UIKit
class ViewController: UIViewController {
    //Outlets/////////////////////////////////
    @IBOutlet weak var clickOutput: UILabel!
    @IBOutlet weak var multOutput: UILabel!
    @IBOutlet weak var messageOutput: UILabel!
    //Variables/////////////////////////////////
    @objc var click = 0
    var mult = 1
    @IBAction func clickMeButton(_ sender: Any) {
        click = click + (1 * mult)
        clickOutput.text = "\(click)"
    }
    @IBAction func twoX(_ sender: Any) {
        if ((click >= 100) && (mult == 1)) {
            mult = 2
            click = click - 100
            multOutput.text = "\(mult)"
            clickOutput.text = "\(click)"
            messageOutput.text = "Nice Job! Keep Going!"
        } else {
            error()
        }
    }
    @IBAction func fourX(_ sender: Any) {
        if ((click >= 1000) && (mult == 2)) {
            mult = 4
            click = click - 1000
            clickOutput.text = "\(click)"
            multOutput.text = "\(mult)"
            messageOutput.text = "Getting closer, Keep Going!"
        } else {
            error()
        }
    }
    @IBAction func eightX(_ sender: Any) {
        if ((click >= 10000) && (mult == 4)) {
            mult = 8
            click = click - 10000
            clickOutput.text = "\(click)"
            multOutput.text = "\(mult)"
            messageOutput.text = "You Did It! Now Keep Playing To See How High You Can Go"
        } else {
            error()
        }
    }
    @IBAction func sixteenX(_ sender: Any) {
        if ((click >= 100000) && (mult == 8)) {
            mult = 16
            click = click - 100000
            clickOutput.text = "\(click)"
            multOutput.text = "\(mult)"
            messageOutput.text = "You Did It! Now Keep Playing To See How High You Can Go"
        } else {
            error()
        }
    }
    @IBAction func thirtytwox(_ sender: Any) {
        if ((click >= 1000000) && (mult == 16)) {
            mult = 32
            click = click - 1000000
            clickOutput.text = "\(click)"
            multOutput.text = "\(mult)"
            messageOutput.text = "You Did It! Now Keep Playing To See How High You Can Go"
        } else {
            error()
        }
    }
    @IBAction func sixtyfourX(_ sender: Any) {
        if ((click >= 10000000) && (mult == 32)) {
            mult = 64
            click = click - 10000000
            clickOutput.text = "\(click)"
            multOutput.text = "\(mult)"
            messageOutput.text = "You Did It! Now Keep Playing To See How High You Can Go"
        } else {
            error()
        }
    }
    
    func error() {
        messageOutput.text = "Not enough Clicks, keep going!"
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        clickOutput.text = "\(click)"
        multOutput.text = "\(mult)"
        messageOutput.text = ""
    }
}
1

There are 1 answers

0
KOTTAIMUTHU SHRINITHI student On

I think this would work

Option 1: Add the first code snippet to the end of your code

func updateUI(){
clickOutput.text = "\(click)"
        }
        
        
        
then at your

@IBAction func clickMeButton(_ sender: Any) {
        click = click + (1 * mult)
        clickOutput.text = "\(click)"
        
       }

change it to this

@IBAction func clickMeButton(_ sender: Any) {
            click = click + (1 * mult)
            clickOutput.text = "\(click)"
            updateUI()
       }

Option 2: Another idea is making updateUI() do the counting too, and also, you should use an operator, += , to add to the number if you want to

func updateUI() { 
  click += 1
  clickOutput.text = "\(click)"
}

Next step is the same as step 3 in Option 1