需要帮助制作 Swift Cookie Clicker 游戏自动保存

Posted

技术标签:

【中文标题】需要帮助制作 Swift Cookie Clicker 游戏自动保存【英文标题】:Need help making Swift Cookie Clicker Game Automatically Save 【发布时间】:2020-10-07 02:27:12 【问题描述】:

我是一名刚学习编程的 9 年级学生。在我掌握了基础知识之后,我想我会用 swift 制作一个 cookie clicker 游戏。我做到了这一点,并且仍在努力改进它并使其更具可玩性,但我无法让它自动保存。我尝试过粘贴和修改其他一些行,但我做错了。我希望它保存变量 click 并在更新时自动 mult 。任何帮助表示赞赏,谢谢。

//
//  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】:

我认为这可行

选项 1:将第一个代码 sn-p 添加到代码末尾

func updateUI()
clickOutput.text = "\(click)"
        
        
        
        
然后在你的

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

改成这个

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

选项 2:另一个想法是让 updateUI() 也进行计数,此外,如果您愿意,您应该使用运算符 += 来添加数字

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

下一步与选项1中的步骤3相同

【讨论】:

以上是关于需要帮助制作 Swift Cookie Clicker 游戏自动保存的主要内容,如果未能解决你的问题,请参考以下文章

Swift 4 WKWebView cookie 获取和删除同步问题

在 Swift 中形成旋转矩阵时需要帮助

iOS Swift WKWebView 维护用户会话 Cookie

如何通过 Sprite Kit 中的 CGPoints 数组制作动画 - Swift

需要帮助更改在 didMoveToView 中调用的 SKSpriteNode 的速度、方向和生成时间

UISlider 的自定义形状并在 Swift 5 中更新进度