制作游戏结束场景

Make a Game Over Scene

在我的游戏中,我希望在红灯动画期间触摸屏幕时出现游戏结束标签。当绿灯动画关闭时,红灯动画打开。当玩家在红灯动画期间触摸屏幕时,我希望游戏屏幕暂停并显示游戏结束标签。到目前为止我有这个,但是当我尝试 运行 它时应用程序崩溃了。

错误信息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: name:'(null)' text:'Game Over! Tap to Play Again' fontName:'Helvetica' position:{1024, 768}'

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */


    if gameOverLabel.parent == nil
    {
        self.addChild(gameOverLabel)
    }

    println(score)

    scoreLabel.text = "\(score)"
}

试试这个。您将 gameOverLabel 设置为字体太大且框架在屏幕外。这就是它不可见的原因。当游戏结束时你也必须停止游戏。

if (!self.paused)
{
    if isGreenLightON
    {
        score += 50
    }
    else
    {
        self.paused = true

        if (gameOverLabel.parent != nil)
        {
            gameOverLabel.removeFromParent()
        }
        gameOverLabel.fontName = "Helvetica"
        gameOverLabel.fontColor = UIColor.blackColor()
        gameOverLabel.fontSize = 24
        gameOverLabel.text = "Game Over! Tap to Play Again"
        gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height - 50)
        self.addChild(gameOverLabel)
    }
}
else
{
    score = 0
    if (gameOverLabel.parent != nil)
    {
        gameOverLabel.removeFromParent()
    }
    self.paused = false
}
scoreLabel.text = "\(score)"