2015-12-02 76 views
1

每當(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. 
} 


} 

任何幫助,將不勝感激。

回答

0

您從未真正將值分配給highscored,因此它始終設置爲您最初聲明爲的0值。

因此,表達式if (highscored > count)永遠不會返回true,因此您將永遠不會執行您之後的代碼。你可能的場景是數量大於零(如果用戶點擊按鈕),這將觸發if語句,或者該計數等於高分(如果用戶完全不點擊按鈕),此時程序將失敗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語句,因爲首先檢查高分數是否大於計數,然後檢查它是否大於或等於在以下else部分中計數的下一個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) 
     } 
    } 
} 

希望這有助於

+1

感謝的人!!!! YESSS – di477