使用 LabelNode 在 Sprite-Kit (Swift) 中切换场景
Switch between Scenes in Sprite-Kit (Swift) using LabelNode
我将 SKLabelNodes
用作按钮,并在菜单助手中使用 if
来检测按钮上的触摸。打印有效(检测也有效),但如何正确切换到我的游戏场景?
func menuHelper(touches: NSSet) {
for touch in touches {
let nodeAtTouch = self.nodeAtPoint(touch.locationInNode(self))
if nodeAtTouch.name == "title" {
print("Title pressed")
}
else if nodeAtTouch.name == "newGame" {
let scene = GameScene()
}
}
}
你只需要制作一个新场景(和过渡,但这是可选的)并使用适当的 SKView 方法呈现它:
else if nodeAtTouch.name == "newGame" {
let transition = SKTransition.fadeWithDuration(1.0)
let nextScene = GameScene(size: scene!.size)
nextScene.scaleMode = .AspectFill //set the scale mode like you did in your view controller
scene?.view?.presentScene(nextScene, transition: transition)
}
没有转换,您只需使用 presentScene method instead of presentScene:transition
我将 SKLabelNodes
用作按钮,并在菜单助手中使用 if
来检测按钮上的触摸。打印有效(检测也有效),但如何正确切换到我的游戏场景?
func menuHelper(touches: NSSet) {
for touch in touches {
let nodeAtTouch = self.nodeAtPoint(touch.locationInNode(self))
if nodeAtTouch.name == "title" {
print("Title pressed")
}
else if nodeAtTouch.name == "newGame" {
let scene = GameScene()
}
}
}
你只需要制作一个新场景(和过渡,但这是可选的)并使用适当的 SKView 方法呈现它:
else if nodeAtTouch.name == "newGame" {
let transition = SKTransition.fadeWithDuration(1.0)
let nextScene = GameScene(size: scene!.size)
nextScene.scaleMode = .AspectFill //set the scale mode like you did in your view controller
scene?.view?.presentScene(nextScene, transition: transition)
}
没有转换,您只需使用 presentScene method instead of presentScene:transition