ios - Swift SpriteKit 标签没有被删除

标签 ios iphone swift sprite-kit

我有 2 个标签,当玩家失败时添加;但是,重新启动游戏时,仍保留 1 个标签。我没有看到任何标签可能被意外复制的代码。

添加标签:

func getScores(x: SKScene) {

    //HighScore
    if currentScore > highScore
    {
        Defaults.setInteger(currentScore, forKey: "High Score")
        //High Score Particles
        //highScoreParticle()
    }

    //High Score
    highScoreLabel      = UILabel(frame: CGRect(x: x.frame.midX - 100 , y: x.frame.midY + 70, width: 200, height: 50))
    highScoreLabel.textAlignment = NSTextAlignment.Center
    highScoreLabel.text = "HighScore: \(Defaults.valueForKey("High Score")!)"
    highScoreLabel.font = UIFont(name: "Helvetica Neue UltraLight", size: 30)
    highScoreLabel.textColor = UIColor.whiteColor()
    x.view?.addSubview(highScoreLabel)

    //Current Score
    currentScoreLabel      = UILabel(frame: CGRect(x: x.frame.midX - 50 , y:  x.frame.midY, width: 100, height: 50))
    currentScoreLabel.textAlignment = NSTextAlignment.Center
    currentScoreLabel.text = "Score: \(currentScore)"
    currentScoreLabel.font = UIFont(name: "Helvetica Neue UltraLight", size: 30)
    currentScoreLabel.textColor = UIColor.whiteColor()
    x.view?.addSubview(currentScoreLabel)
}

删除标签:

func resetGame(scene: SKScene) {

    //Removes Labels
    startLabel.removeFromSuperview()
    currentScoreLabel.removeFromSuperview()
    highScoreLabel.removeFromSuperview()

    //Remove everything off scene
    scene.removeAllChildren()
    scene.removeAllActions()

    //Reset Variables
    currentScore = 0
}

highScoreLabel 不会从场景中删除

最佳答案

您正在将 UILabel 添加到场景 View Controller 。场景无法控制这一点。您需要创建一个 SKLabelNode 并使用 addChild

将其添加到场景中

关于ios - Swift SpriteKit 标签没有被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34772618/

相关文章:

ios - Ekevents 在日历中多次添加?

ios - 避免检测终点导航

ios - 当应用程序处于后台甚至被用户终止时,在 iOS 上无需移动即可获取用户位置

objective-c - 重新加载 View Controller 后自动旋转不起作用

ios - 当我调用 URLSession 任务 task.cancel() 时会发生什么?

iOS8 和 Safari 不再支持蓝牙扫描仪

ios - URLSession 凭据缓存允许使用不正确的凭据进行身份验证

ios - 如何从 iOS 应用程序中的 Safari url 获取响应数据?

ios - Xcode 调试 View 层次结构显示空白屏幕

macos - Swift - 是否可以在 MacOS ManuBar 应用程序中的 AppDelegate 类之外构建菜单栏项?