转换回gamescene.swift会导致错误"'尝试添加已经拥有父级的SKNode"

时间:2015-02-25 06:12:40

标签: swift transition parent sknode

我的应用包含两个场景。 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}'

***首先抛出调用堆栈:

3 个答案:

答案 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时它死亡。