每當(count)大於(highscored)時,我會得到相同的消息。Swift遊戲相同的消息
但我想要的是什麼時候(計數)低於(高分)你得到 (「你得分(點數)」)。
我是一名學生,做過關於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.
}
}
任何幫助,將不勝感激。
感謝的人!!!! YESSS – di477