在swift中添加得分和高分

时间:2015-04-08 06:38:46

标签: ios swift

我正在制作带有图片的测验游戏。

我正在尝试添加分数。总是当你按下正确答案时,你得到一个分数,当你按错了答案时,你必须重新开始游戏(我已经完成了)。

我的问题是,无论我在哪里尝试使用此代码,它都会出现各种错误。

所以这是高分的代码:

        Score++
        ScoreLabel.text = NSString(format: "Score: %i", Score)
        if (Score > HighScore){
            HighScore = Score
            HighScoreLabel.text = NSString(format: "HighScore : %i", HighScore)

我把它放在这个地方代码:

import UIKit

类ViewController:UIViewController {

@IBOutlet var QuestionImage : UIImageView!
@IBOutlet var QuestionLabel: UILabel!
@IBOutlet var Button1: UIButton!
@IBOutlet var Button2: UIButton!
@IBOutlet var Button3: UIButton!
@IBOutlet var Button4: UIButton!
@IBOutlet var Restart: UIButton!
@IBOutlet var LabelEnd: UILabel!
@IBOutlet var ScoreLabel: UILabel!
@IBOutlet var HighScoreLabel: UILabel!



var CorrectAnswer = String()



var Score = 0

var HighScore = 0

  override func viewDidLoad() {



    super.viewDidLoad()



    // Do any additional setup after loading the view, typically from a nib.



    Hide()



    RandomQuestions()



    HideRestart()



}



override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }



func RandomQuestions(){



    var RandomNumber = arc4random() % 2

    RandomNumber += 1

    switch(RandomNumber){



    case 1:

      QuestionImage.image = UIImage(named: (“image.jpg"))

     QuestionLabel.text = “question?"

        Button1.setTitle(“answer", forState: UIControlState.Normal)

        Button2.setTitle(“answer", forState: UIControlState.Normal)

        Button3.setTitle(“answer", forState: UIControlState.Normal)

        Button4.setTitle(“answer", forState: UIControlState.Normal)

        CorrectAnswer = "1"

        break





    case 2:

        QuestionImage.image = UIImage(named: (“image2.jpg"))

       QuestionLabel.text = “question?"

        Button1.setTitle(“answer", forState: UIControlState.Normal)

         Button2.setTitle(“answer", forState: UIControlState.Normal)

        Button3.setTitle(“answer", forState: UIControlState.Normal)

        Button4.setTitle(“answer", forState: UIControlState.Normal)

        CorrectAnswer = "3"



        break



        default:



        break

    }



}

    func Hide(){



    LabelEnd.hidden = true



}



func UnHide(){



    LabelEnd.hidden = false



}

   func HideButtons(){

    Button1.hidden = true



    Button2.hidden = true



    Button3.hidden = true



    Button4.hidden = true



}

  func HideRestart(){



    Restart.hidden = true

     }



func UnHideRestart(){



    Restart.hidden = false

     }



@IBAction func Button1Action(sender: AnyObject) {

        UnHide()

        if (CorrectAnswer == "1"){

        RandomQuestions()

        Hide()

        HideRestart()

        Score++
        ScoreLabel.text = NSString(format: "Score: %i", Score)
        if (Score > HighScore){
            HighScore = Score
            HighScoreLabel.text = NSString(format: "HighScore : %i", HighScore)




    }



    else{

        LabelEnd.text = "You are wrong!"

        HideButtons()

        UnHideRestart()

       }

   }



@IBAction func Button2Action(sender: AnyObject) {

    UnHide()

      if (CorrectAnswer == "2"){

      RandomQuestions()

        Hide()

        HideRestart()


        Score++
        ScoreLabel.text = NSString(format: "Score: %i", Score)
        if (Score > HighScore){
            HighScore = Score
            HighScoreLabel.text = NSString(format: "HighScore : %i", HighScore)

    }



    else{

        LabelEnd.text = "You are wrong!"

        HideButtons()

        UnHideRestart()



    }



}

@IBAction func Button3Action(sender: AnyObject) {



    UnHide()



    if (CorrectAnswer == "3"){

        RandomQuestions()

        Hide()

        HideRestart()


        Score++
        ScoreLabel.text = NSString(format: "Score: %i", Score)
        if (Score > HighScore){
            HighScore = Score
            HighScoreLabel.text = NSString(format: "HighScore : %i", HighScore)


    }



    else{



        LabelEnd.text = "You are wrong!"

        HideButtons()

        UnHideRestart()



    }

}

@IBAction func Button4Action(sender: AnyObject) {



    UnHide()

        if (CorrectAnswer == "4"){

       RandomQuestions()

        Hide()

        HideRestart()

        Score++
        ScoreLabel.text = NSString(format: "Score: %i", Score)
        if (Score > HighScore){
            HighScore = Score
            HighScoreLabel.text = NSString(format: "HighScore : %i", HighScore)


        }



    else{



        LabelEnd.text = "You are wrong!"

        HideButtons()

        UnHideRestart()



    }



}

}

总是当你按下右键时它的分数总是出错,它给出了一个标签"你错了!"。

这些问题存在这种情况,最后还有CorrectAnswer。

我有4个答案按钮。我还有2个视图控制器,其他是开始屏幕,第二个是游戏,我想在开始视图上获得高分标签,在游戏视图中获得分数标签。

非常感谢你的正手!

1 个答案:

答案 0 :(得分:1)

所有ButtonXAction个函数都缺少其右括号)。复制粘贴时你可能错过了一行...