swift - SpriteKit - 问答游戏传递一系列问题和答案
问题描述
我想知道如何实施以下内容:
创建一系列问题,并管理正确答案,即
var question = ["What colour is the sky?"]
var answers = ["Blue", "Yellow", "Black", "Green"]
我已经设置了 SpriteKit 的逻辑,用户可以在其中点击等。但是,我似乎无法在网上找到任何可以帮助我的东西。
本质上,我想显示一个问题,并为每个问题生成一个 SKLabelNode 的答案。如果答案正确,则进入下一个问题。我只是不明白该怎么做。任何和所有的帮助将不胜感激。
解决方案
您需要一个模型来解决您的问题:
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
显然,您必须添加逻辑才能使其正常工作。
推荐阅读
- hbase - 如何在 Phoenix sqlline 中获取 md5sum 的十六进制字符串?
- javascript - 如果选中收音机,则 Kendo Ui 禁用列
- android - 遇到了问题。解锁设备后不调用 OnStart Onresume 方法
- php - 找不到 php-gds-session 类 LookupRequest
- unit-testing - Vala 中的单元测试
- r - 是否有用于刷新数据文件或数据框以自动获取最近更新的数据的 R 函数?
- c++ - 如何在 C++ 中将 Tesseract-OCR 与 OpenCV 集成
- reactjs - 如何在 reactjs 中使用按钮 onClick?
- mongodb - 如何在 Windows PowerShell 中使用 mongoimport?
- macos - 新更新后 Visual Studio for Mac 损坏 - .NET Core 2.2 SDK 不兼容