首页 > 解决方案 > SpriteKit - 问答游戏传递一系列问题和答案

问题描述

我想知道如何实施以下内容:

创建一系列问题,并管理正确答案,即

var question = ["What colour is the sky?"]

var answers = ["Blue", "Yellow", "Black", "Green"]

我已经设置了 SpriteKit 的逻辑,用户可以在其中点击等。但是,我似乎无法在网上找到任何可以帮助我的东西。

本质上,我想显示一个问题,并为每个问题生成一个 SKLabelNode 的答案。如果答案正确,则进入下一个问题。我只是不明白该怎么做。任何和所有的帮助将不胜感激。

标签: swift

解决方案


您需要一个模型来解决您的问题:

class Question {
 
  let question: String
  let answerA: String
  let answerB: String
  let answerC: String
  let answerD: String
  let correctAnswer: Int
    
  init(questionText: String, choiceA: String, choiceB: String, choiceC:String, choiceD:String, answer: Int){
    question = questionText
    answerA = choiceA
    answerB = choiceB
    answerC = choiceC
    answerD = choiceD
    correctAnswer = answer
    
  } 
}

还有一个 QuestionBank 类:

class QuestionBank {

   var list = [Question]()

   init(){
     
     list.append(Question(questionText:"What colour is the sky?", choiceA: "Blue", choiceB: "Yellow", choiceC: "Black", choiceD: "Green", answer: 1))

     //Add more questions to your array here

   }
  
 }

在您的 ViewController 类中:

  let questions = QuestionBank()
  var questionNumber = 0
  var correctButtonAnswer = 0

  questionLabel.text = questions.list[questionNumber].question
  answerAButton.setTitle(questions.list[questionNumber].answerA, for: UIControl.State.normal)
  answerBButton.setTitle(questions.list[questionNumber].answerB, for: UIControl.State.normal)
  answerCButton.setTitle(questions.list[questionNumber].answerC, for: UIControl.State.normal)
  answerDButton.setTitle(questions.list[questionNumber].answerD, for: UIControl.State.normal)
  correctButtonAnswer = questions.list[questionNumber}.correctAnswer 
 

显然,您必须添加逻辑才能使其正常工作。


推荐阅读