如何在 SKScene 中正确设置 UIButton
How to proper set up a UIButton in SKScene
我在我的 Scene/View 中设置了一个 UIButton,当 ViewController 调用场景时该按钮会显示。问题是当我单击 Button 并调用我的 GameScene 时,Button 仍然存在。我想我设置按钮的方式有误。
我想问题是我无法在我的 startGame
函数中调用 button1 上的 removeFromSuperview()
函数。
我该如何解决这个问题?感谢您的帮助!
import Foundation
import SpriteKit
import UIKit
class MenuScene: SKScene {
override init(size: CGSize) {
super.init(size: size)
backgroundColor = SKColor.grayColor()
let label = SKLabelNode(fontNamed: "CourierNewPS-BoldMT")
label.text = "Start Game"
label.fontSize = 40
label.fontColor = SKColor.blackColor()
label.position = CGPoint(x: size.width/2, y: size.height/2)
addChild(label)
}
override func didMoveToView(view: SKView) {
let button1=UIButton(frame: CGRectMake(size.width/2, size.height/2, 300, 100))
button1.backgroundColor = UIColor.greenColor()
button1.setTitleColor(UIColor.blackColor(), forState: .Normal)
button1.setTitle("Unfocused", forState: .Normal)
button1.setTitle("Start", forState: .Focused)
button1.addTarget(self, action: "startGame:", forControlEvents: UIControlEvents.PrimaryActionTriggered)
self.view?.addSubview(button1)
}
func startGame(sender:UIButton) {
let gameView = view! as SKView
gameView.ignoresSiblingOrder = true
let reveal = SKTransition.flipHorizontalWithDuration(0.2)
let scene = GameScene(size: self.size)
gameView.presentScene(scene, transition:reveal)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
您可以使用 sender
从 startGame(sender:UIButton)
方法访问您的 button1
对象。在这种情况下,它是对 button1
的引用。您可以从中调用 removeFromSuperview
方法:
func startGame(sender:UIButton) {
let gameView = view! as SKView
gameView.ignoresSiblingOrder = true
let reveal = SKTransition.flipHorizontalWithDuration(0.2)
let scene = GameScene(size: self.size)
gameView.presentScene(scene, transition:reveal)
sender.removeFromSuperview()
}
我在我的 Scene/View 中设置了一个 UIButton,当 ViewController 调用场景时该按钮会显示。问题是当我单击 Button 并调用我的 GameScene 时,Button 仍然存在。我想我设置按钮的方式有误。
我想问题是我无法在我的 startGame
函数中调用 button1 上的 removeFromSuperview()
函数。
我该如何解决这个问题?感谢您的帮助!
import Foundation
import SpriteKit
import UIKit
class MenuScene: SKScene {
override init(size: CGSize) {
super.init(size: size)
backgroundColor = SKColor.grayColor()
let label = SKLabelNode(fontNamed: "CourierNewPS-BoldMT")
label.text = "Start Game"
label.fontSize = 40
label.fontColor = SKColor.blackColor()
label.position = CGPoint(x: size.width/2, y: size.height/2)
addChild(label)
}
override func didMoveToView(view: SKView) {
let button1=UIButton(frame: CGRectMake(size.width/2, size.height/2, 300, 100))
button1.backgroundColor = UIColor.greenColor()
button1.setTitleColor(UIColor.blackColor(), forState: .Normal)
button1.setTitle("Unfocused", forState: .Normal)
button1.setTitle("Start", forState: .Focused)
button1.addTarget(self, action: "startGame:", forControlEvents: UIControlEvents.PrimaryActionTriggered)
self.view?.addSubview(button1)
}
func startGame(sender:UIButton) {
let gameView = view! as SKView
gameView.ignoresSiblingOrder = true
let reveal = SKTransition.flipHorizontalWithDuration(0.2)
let scene = GameScene(size: self.size)
gameView.presentScene(scene, transition:reveal)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
您可以使用 sender
从 startGame(sender:UIButton)
方法访问您的 button1
对象。在这种情况下,它是对 button1
的引用。您可以从中调用 removeFromSuperview
方法:
func startGame(sender:UIButton) {
let gameView = view! as SKView
gameView.ignoresSiblingOrder = true
let reveal = SKTransition.flipHorizontalWithDuration(0.2)
let scene = GameScene(size: self.size)
gameView.presentScene(scene, transition:reveal)
sender.removeFromSuperview()
}