我的应用包含两个场景。 Playscene.swift和gamescene.swift。
从gamescene过渡到playcene(玩游戏的地方)完美无缺。然而,一旦达到游戏结束,我就会出现一个“重播”按钮,允许用户返回到gamescene.swift。在转换回来时,它崩溃并出现错误“尝试添加已有父级的SKNode”。是否有正确的方法转换回主屏幕或重新启动游戏,所以我没有收到错误?谢谢你的帮助!!
if self.nodeAtPoint(touchLocation) == self.replay {
let scene = GameScene(size: self.size)
let skView = self.view as SKView!
scene.size = skView.bounds.size
self.view?.presentScene(scene)
let transition = SKTransition.crossFadeWithDuration(1.0)
transition.pausesOutgoingScene = false
skView.presentScene(scene, transition: transition)
}
}
Gamescene.swift错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKLabelNode> name:'(null)' text:'Highscore:' fontName:'Avenir-Black' position:{344, 549}'
***首先抛出调用堆栈:
答案 0 :(得分:1)
答案 1 :(得分:0)
您试图两次呈现相同的场景:
self.view?.presentScene(scene)
skView.presentScene(scene, transition: transition)
也许你不想这样做:
if self.nodeAtPoint(touchLocation) == self.replay {
let skView = self.view as SKView!
let scene = GameScene(size: skView.bounds.size)
// Might also work with the following line instead :
// let scene = GameScene(size: self.frame.size)
let transition = SKTransition.crossFadeWithDuration(1.0)
transition.pausesOutgoingScene = false
self.view?.presentScene(scene, transition: transition)
}
答案 2 :(得分:0)
您可能在GameScene class
之外声明SKLabelNode,使SKLabelNode成为全局,您应该在类中声明它,以便在scene A
转换为scene B
时它死亡。