创建的按钮出现在游戏中的其他场景中
Created Buttons appear in other scenes within the Game
// 按钮 class
class 按钮:UIButton {
override init(frame: CGRect){
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setup(){
backgroundColor = #colorLiteral(red: 0.1215686277, green: 0.01176470611, blue: 0.4235294163, alpha: 1)
layer.cornerRadius = 12
layer.masksToBounds = false
translatesAutoresizingMaskIntoConstraints = false
}
}
// 创建按钮对象的Class
构造问题{
var 问题:字符串!
var 答案:[字符串]!
var AnswerNumber:整数!
}
class LevelOne:SKScene {
var button = Button()
var buttons = [Button]()
let Question_met = UILabel(frame: CGRect(x: 0.3, y: 0.3, width: 40, height: 21))
var Questions = [Question]()
var QNumber = Int()
var buttonNames = [""]
var AnsNumber = Int()
let selectors:[Selector] = [#selector(handleButton1), #selector(handleButton2), #selector(handleButton3),#selector(handleButton4)]
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background")
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
questionFunction()
buttonFuction()
pickQuestion()
}
@objc func questionFunction(){
Questions = [Question(Question: "What is a BMW ?", Answers: ["Bat","Cat","Vehicle","Pat"], AnswerNumber: 2),
Question(Question: "What is a Boeing ?", Answers: ["Bat","Aircraft","Cat","Pat"], AnswerNumber: 1),
Question(Question: "What is a Ant ?", Answers: ["Insect","Cat","Insect","Nate"], AnswerNumber: 2),
Question(Question: "What is a Apple ?", Answers: ["Fruit","Cat","bat","Pat"], AnswerNumber: 0),
Question(Question: "Where is london ?", Answers: ["UK","USA","France","Germany"], AnswerNumber: 0),
Question(Question: "Where is New York ?", Answers: ["Canada","USA","France","Germany"], AnswerNumber: 2),
Question(Question: "where is Berlin ?", Answers: ["UK","USA","Italy","Germany "], AnswerNumber: 3),
Question(Question: "Where is Toronto ?", Answers: ["India","Africa","Canada","Norway"], AnswerNumber: 2),
Question(Question: "Where is Rome ?", Answers: ["Japan","China","Italy","Ireland"], AnswerNumber: 2),
Question(Question: "where is Chennai ?", Answers: ["India","Brazil","Swiss","Germany"], AnswerNumber: 0),]
Question_met.frame = CGRect(x: 330, y: 70, width: 200, height: 21)
Question_met.backgroundColor = UIColor.orange
Question_met.textColor = UIColor.white
Question_met.textAlignment = NSTextAlignment.center
Question_met.text = "What caused Global Warming ?"
self.view?.addSubview(Question_met)
}
@objc func buttonFuction(){
let stacView = UIStackView()
stacView.spacing = 12
stacView.distribution = .fillEqually
stacView.axis = .horizontal
stacView.translatesAutoresizingMaskIntoConstraints = false
view!.addSubview(stacView)
buttonNames = ["One","Two","Three","Four"]
for i in 0..<4{
button = Button()
button.setTitle(buttonNames[i], for: .normal)
stacView.addArrangedSubview(button)
buttons.append(button)
button.addTarget(self, action: selectors[i], for: .touchUpInside)
}
NSLayoutConstraint.activate([stacView.centerXAnchor.constraint(equalTo: view!.centerXAnchor),stacView.centerYAnchor.constraint(equalTo: view!.centerYAnchor),stacView.widthAnchor.constraint(equalToConstant: 350),stacView.heightAnchor.constraint(equalToConstant:70)])
}
@objc func pickQuestion(){
if Questions.count > 0{
QNumber = 0
Question_met.text = Questions[QNumber].Question
AnsNumber = Questions[QNumber].AnswerNumber
for i in 0..<buttons.count{
buttons[i].setTitle(Questions[QNumber].Answers[i], for: .normal)
}
Questions.remove(at: QNumber)
}
else {
print("Done!")
let sceneToMoveTo = Instructions(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}
}
@objc func handleButton1() {
if AnsNumber == 0 {
pickQuestion()
print("Correct!")
}
else {
print("You are Wrong!!!")
}
}
@objc func handleButton2(){
if AnsNumber == 1 {
pickQuestion()
print("Correct!")
}
else {
print("You are Wrong!!!")
}
}
@objc func handleButton3(){
if AnsNumber == 2 {
pickQuestion()
print("Correct!")
}
else {
print("You are Wrong!!!")
}
}
@objc func handleButton4(){
if AnsNumber == 3 {
pickQuestion()
print("Correct!")
}
else {
print("You are Wrong!!!")
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
}
我目前正在开发一款游戏,其中包含各种关卡和菜单场景。当前级别创建了一个问题标签和答案按钮,即使在级别完成并且您导航到另一个场景后,无论您导航到哪个场景,按钮和问题都会保留在场景中。
我希望得到一个解决方案,让按钮和标签只出现在指定的场景中,而不出现在其他场景中,除非有说明。
提前谢谢你。
我认为您不了解此处的所有权模型。 ViewController 拥有它的视图。视图拥有 SKView,SKView 拥有 SKScene。有一个从 SKScene 到它的 SKView 的后向指针,您正在使用它来将 UIKit StakcView 添加到场景的父视图。由于 UIKit 位于 SKScene 之上,因此您的按钮始终可见。这里正确的架构是视图应该添加和管理它自己的子视图。每当它加载一个新的 SKScene 时,它应该确定该场景需要什么 UI,然后隐藏任何不应显示的 UI。您只需在相关视图上设置 .isHidden 即可。
// 按钮 class
class 按钮:UIButton {
override init(frame: CGRect){
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setup(){
backgroundColor = #colorLiteral(red: 0.1215686277, green: 0.01176470611, blue: 0.4235294163, alpha: 1)
layer.cornerRadius = 12
layer.masksToBounds = false
translatesAutoresizingMaskIntoConstraints = false
}
}
// 创建按钮对象的Class
构造问题{ var 问题:字符串! var 答案:[字符串]! var AnswerNumber:整数! }
class LevelOne:SKScene {
var button = Button()
var buttons = [Button]()
let Question_met = UILabel(frame: CGRect(x: 0.3, y: 0.3, width: 40, height: 21))
var Questions = [Question]()
var QNumber = Int()
var buttonNames = [""]
var AnsNumber = Int()
let selectors:[Selector] = [#selector(handleButton1), #selector(handleButton2), #selector(handleButton3),#selector(handleButton4)]
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background")
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
questionFunction()
buttonFuction()
pickQuestion()
}
@objc func questionFunction(){
Questions = [Question(Question: "What is a BMW ?", Answers: ["Bat","Cat","Vehicle","Pat"], AnswerNumber: 2),
Question(Question: "What is a Boeing ?", Answers: ["Bat","Aircraft","Cat","Pat"], AnswerNumber: 1),
Question(Question: "What is a Ant ?", Answers: ["Insect","Cat","Insect","Nate"], AnswerNumber: 2),
Question(Question: "What is a Apple ?", Answers: ["Fruit","Cat","bat","Pat"], AnswerNumber: 0),
Question(Question: "Where is london ?", Answers: ["UK","USA","France","Germany"], AnswerNumber: 0),
Question(Question: "Where is New York ?", Answers: ["Canada","USA","France","Germany"], AnswerNumber: 2),
Question(Question: "where is Berlin ?", Answers: ["UK","USA","Italy","Germany "], AnswerNumber: 3),
Question(Question: "Where is Toronto ?", Answers: ["India","Africa","Canada","Norway"], AnswerNumber: 2),
Question(Question: "Where is Rome ?", Answers: ["Japan","China","Italy","Ireland"], AnswerNumber: 2),
Question(Question: "where is Chennai ?", Answers: ["India","Brazil","Swiss","Germany"], AnswerNumber: 0),]
Question_met.frame = CGRect(x: 330, y: 70, width: 200, height: 21)
Question_met.backgroundColor = UIColor.orange
Question_met.textColor = UIColor.white
Question_met.textAlignment = NSTextAlignment.center
Question_met.text = "What caused Global Warming ?"
self.view?.addSubview(Question_met)
}
@objc func buttonFuction(){
let stacView = UIStackView()
stacView.spacing = 12
stacView.distribution = .fillEqually
stacView.axis = .horizontal
stacView.translatesAutoresizingMaskIntoConstraints = false
view!.addSubview(stacView)
buttonNames = ["One","Two","Three","Four"]
for i in 0..<4{
button = Button()
button.setTitle(buttonNames[i], for: .normal)
stacView.addArrangedSubview(button)
buttons.append(button)
button.addTarget(self, action: selectors[i], for: .touchUpInside)
}
NSLayoutConstraint.activate([stacView.centerXAnchor.constraint(equalTo: view!.centerXAnchor),stacView.centerYAnchor.constraint(equalTo: view!.centerYAnchor),stacView.widthAnchor.constraint(equalToConstant: 350),stacView.heightAnchor.constraint(equalToConstant:70)])
}
@objc func pickQuestion(){
if Questions.count > 0{
QNumber = 0
Question_met.text = Questions[QNumber].Question
AnsNumber = Questions[QNumber].AnswerNumber
for i in 0..<buttons.count{
buttons[i].setTitle(Questions[QNumber].Answers[i], for: .normal)
}
Questions.remove(at: QNumber)
}
else {
print("Done!")
let sceneToMoveTo = Instructions(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}
}
@objc func handleButton1() {
if AnsNumber == 0 {
pickQuestion()
print("Correct!")
}
else {
print("You are Wrong!!!")
}
}
@objc func handleButton2(){
if AnsNumber == 1 {
pickQuestion()
print("Correct!")
}
else {
print("You are Wrong!!!")
}
}
@objc func handleButton3(){
if AnsNumber == 2 {
pickQuestion()
print("Correct!")
}
else {
print("You are Wrong!!!")
}
}
@objc func handleButton4(){
if AnsNumber == 3 {
pickQuestion()
print("Correct!")
}
else {
print("You are Wrong!!!")
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
}
我目前正在开发一款游戏,其中包含各种关卡和菜单场景。当前级别创建了一个问题标签和答案按钮,即使在级别完成并且您导航到另一个场景后,无论您导航到哪个场景,按钮和问题都会保留在场景中。
我希望得到一个解决方案,让按钮和标签只出现在指定的场景中,而不出现在其他场景中,除非有说明。
提前谢谢你。
我认为您不了解此处的所有权模型。 ViewController 拥有它的视图。视图拥有 SKView,SKView 拥有 SKScene。有一个从 SKScene 到它的 SKView 的后向指针,您正在使用它来将 UIKit StakcView 添加到场景的父视图。由于 UIKit 位于 SKScene 之上,因此您的按钮始终可见。这里正确的架构是视图应该添加和管理它自己的子视图。每当它加载一个新的 SKScene 时,它应该确定该场景需要什么 UI,然后隐藏任何不应显示的 UI。您只需在相关视图上设置 .isHidden 即可。