简体   繁体   English

从父节点Swift 2移除子节点

[英]Removing child from parent node Swift 2

I am trying to update my score label at the end of the game. 我正在尝试在游戏结束时更新分数标签。 Since the variable (seems to be) outside of the scope I figured I would just remove the label and make a new one in the adjust function. 由于变量(似乎是)超出了我的范围,因此我将删除标签并在adjust函数中创建一个新标签。 I am not familiar with swift and would appreciate help. 我不熟悉swift,将不胜感激。 Can I just reposition or do I have to create new label? 我可以重新定位还是必须创建新标签? Cant figure it out. 无法弄清楚。 Thanks 谢谢

func loadScore() {
    let scoreBand = SKLabelNode(fontNamed: "Arial")
    scoreBand.name = StickHeroGameSceneChildName.ScoreName.rawValue
    scoreBand.text = "0"
    scoreBand.position = CGPointMake(0, DefinedScreenHeight / 2 - 200)
    scoreBand.fontColor = SKColor.whiteColor()
    scoreBand.fontSize = 100
    scoreBand.zPosition = StickHeroGameSceneZposition.ScoreZposition.rawValue
    scoreBand.horizontalAlignmentMode = .Center

    addChild(scoreBand)
}
func adjustScore() {
    //var scoreBand = scoreBand
    scoreBand.position = CGPointMake(0, DefinedScreenHeight / 2 - 100)//doesnt recognize scoreBand

}

You can reposition, just declare scoreBand as a variable of your containing class. 您可以重新定位,只需将scoreBand声明为包含类的变量即可。 For example: 例如:

class SomeClass: SKSpriteNode {

    var scoreBand: SKLabelNode!

    override init(size: CGSize) {
        scoreBand = SKLabelNode(fontNamed: "Arial")

        super.init(size: size)

        anchorPoint = CGPointMake(0.5, 0.5)
        physicsWorld.contactDelegate = self 
    }

    func loadScore() {
        scoreBand.name = StickHeroGameSceneChildName.ScoreName.rawValue
        scoreBand.text = "0"
        scoreBand.position = CGPointMake(0, DefinedScreenHeight / 2 - 200)
        scoreBand.fontColor = SKColor.whiteColor()
        scoreBand.fontSize = 100
        scoreBand.zPosition = StickHeroGameSceneZposition.ScoreZposition.rawValue
        scoreBand.horizontalAlignmentMode = .Center

        addChild(scoreBand)
    }

    func adjustScore() {
        scoreBand.position = CGPointMake(0, DefinedScreenHeight / 2 - 100)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM