ios - swift 游戏相同的消息

标签 ios iphone xcode swift swift2

每当 (count) 大于 (highscored) 时,我都会收到相同的消息。

但我想要的是当 (count) 低于 (highscored) 你得到 (“你得分(计数)分”)。

我是一名学生并且做了一个关于swift的教程,所以现在我添加了一些要学习的东西,它是一个点击游戏,你需要尽可能快地点击以设置一个新的高分。

这就是我要说的部分:

func subtractTime() {
    seconds--
    timerLabel.text = "Time: \(seconds)"

    if(seconds == 0)  {
        if(highscored < count)
        {
            highscored < count
            saveHighScore(count)
            highscore.text = "Highscore: \(loadhighScore().description)"
        }

        if (highscored > count) {
            timer.invalidate()
            let alert = UIAlertController(title: "Time is up!",
                message: "You scored \(count) points",
                preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction (UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler: {
                action in self.setupGame()
            }))
            presentViewController(alert, animated: true, completion:nil)
        }
        else{
        timer.invalidate()
        let alert2 = UIAlertController(title: "Time is up!",
            message: "You set a new higscore \(count) points",
            preferredStyle: UIAlertControllerStyle.Alert)
            alert2.addAction (UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler: {
            action in self.setupGame()
        }))
            presentViewController(alert2, animated: true, completion:nil)}



    }}

这是孔代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet var scoreLabel: UILabel!
@IBOutlet var timerLabel: UILabel!
@IBOutlet var highscore: UILabel!


var count = 0
var seconds = 0
var timer = NSTimer()
var highscored = 0


@IBAction func buttonPressed()  {
    count++
    scoreLabel.text = "Score \n\(count)"
}


func setupGame() { if(highscored > count) {
    seconds = 30
    count = 0
    timerLabel.text = "Time: \(seconds)"
    scoreLabel.text = "Score:\(count)"
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("subtractTime"), userInfo: nil, repeats: true)
    }
else  {
    seconds = 30
    count = 0
    timerLabel.text = "Time: \(seconds)"
    scoreLabel.text = "Score:\(count)"
    highscore.text = "Highscore: \(loadhighScore().description)"
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("subtractTime"), userInfo: nil, repeats: true)
    }
}


func subtractTime() {
    seconds--
    timerLabel.text = "Time: \(seconds)"

    if(seconds == 0)  {
        if(highscored < count)
        {
            highscored < count
            saveHighScore(count)
            highscore.text = "Highscore: \(loadhighScore().description)"
        }

        if (highscored > count) {
            timer.invalidate()
            let alert = UIAlertController(title: "Time is up!",
                message: "You scored \(count) points",
                preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction (UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler: {
                action in self.setupGame()
            }))
            presentViewController(alert, animated: true, completion:nil)
        }
        else{
        timer.invalidate()
        let alert2 = UIAlertController(title: "Time is up!",
            message: "You set a new higscore \(count) points",
            preferredStyle: UIAlertControllerStyle.Alert)
            alert2.addAction (UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler: {
            action in self.setupGame()
        }))
            presentViewController(alert2, animated: true, completion:nil)}



    }}


func saveHighScore(high:Int) {
    NSUserDefaults.standardUserDefaults().setInteger(high, forKey: "highscore")
}
func loadhighScore() -> Int {
    return NSUserDefaults.standardUserDefaults().integerForKey("highscore")
}
func resetHighScore() {
    NSUserDefaults.standardUserDefaults().removeObjectForKey("highscore")
}




override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
setupGame()



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

如有任何帮助,我们将不胜感激。

最佳答案

您实际上从未为 highscored 赋值所以它总是设置为 0您最初将其声明为的值。

因此,表达式 if (highscored > count)永远不会返回 true,所以你永远不会执行你想要的代码。您可能的情况是计数大于零(如果用户完全点击按钮),这将触发 if(highscored < count) if 语句,或者计数等于 highscored(如果用户根本不点击按钮),此时程序将失败 highscored > count表达式,然后转到 else 语句,此时它告诉用户他们有了新的高分。

一个好的解决方案是将 highScored 的值设置为 setupGame() 中的当前最高分。方法,像这样:

func setupGame() { 
    highscored = loadhighscore()
    seconds = 30
    count = 0
    timerLabel.text = "Time: \(seconds)"
    scoreLabel.text = "Score:\(count)"
    if(highscored > count) {
        highscore.text = "Highscore: \(loadhighScore().description)"
    }
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("subtractTime"), userInfo: nil, repeats: true)
}

另一件值得一提的事情是,虽然这不会导致您的代码出现任何错误,但您的 subtractTime() 中似乎有一个多余的 if 语句。方法,因为您首先检查 highscored 是否大于 count,然后您还检查它是否大于或等于以下 else 中的 count下一个 if 语句的一部分。如果这是故意的,那么请忽略下一段代码,但我可能会将其格式化为类似的格式;

func subtractTime() {
    seconds--
    timerLabel.text = "Time: \(seconds)"

    if(seconds == 0)  {
        timer.invalidate()

        if(highscored <= count)
        {
            saveHighScore(count)
            highscore.text = "Highscore: \(loadhighScore().description)"

            let alert2 = UIAlertController(title: "Time is up!",
                message: "You set a new higscore \(count) points",
                preferredStyle: UIAlertControllerStyle.Alert)
            alert2.addAction (UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler: {
                action in self.setupGame()
            }))
                presentViewController(alert2, animated: true, completion:nil)
        }
        else {
            let alert = UIAlertController(title: "Time is up!",
                message: "You scored \(count) points",
                preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction (UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler: {
                action in self.setupGame()
            }))
            presentViewController(alert, animated: true, completion:nil)
        }
    }
}

希望对你有帮助

关于ios - swift 游戏相同的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34038475/

相关文章:

iphone - 滚动到索引

android - 在某些页面上强制方向

iphone - 核心数据是否保存来自不同线程的更改?

iphone - 在 UICollectionView 中创建被子布局

iphone - Xcode Allocations Instrument - 监听内存警告

ios - 如何合并 Framework SwiftUI

ios - 使用 UIDocumentInteractionController 时隐藏状态栏?

iphone - iOS:UIView 相对于 UIWindow 的起源

iphone - 设备中出现声音问题

objective-c - 错误 : writable atomic property cannot pair a synthesized setter/getter with a user defined setter/getter