我正在寻找一个按钮,将我从startScene带到GameScene。我发现的一切都是在单击屏幕而不是按钮时发生过渡。这是我发现的但无法正常工作。有什么想法吗?
var startButton = SKNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
startButton = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 100, height: 50))
startButton.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(startButton)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
let gameSceneTransition = GameScene(fileNamed: "GameScene")
let touch = touches
let location = touch.first!.locationInNode(self)
let node = self.nodeAtPoint(location)
// If next button is touched, start transition to second scene
if (node.name == "startButton") {
self.scene?.view?.presentScene(gameSceneTransition!, transition: SKTransition.fadeWithDuration(1.0))
}
}
最佳答案
如果要比较节点的名称,则需要设置节点的name属性。简单地说:
var startButton = SKNode()
不设置节点的名称属性。你最也说
startButton.name = "startButton"
你可以说
if node == startButton
{
self.scene?.view?.presentScene(gameSceneTransition!, transition: SKTransition.fadeWithDuration(1.0))
}