SKLabelNode will disappear but is still clickable

落爺英雄遲暮 提交于 2019-12-02 05:55:32

You are trying to determine if the constrainPoints' location are being touched. Even if you remove the label from the scene, it is still an object in memory, i.e: you could re-ad it later.. it still has all it's properties including position, etc..

I would try this instead:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    for touch: AnyObject in touches {
        if nodeAtPoint(touch.locationInNode(self)) == lemonadeLabel {
            println("lemonadeLabel pressed")
            lemonadeLabel.removeFromParent()
        }
    }
}

You basically determine if the lemonadeLabel is the node at that position, if yes you remove it. Since you compare with the added node in the scene, if it's gone, it will not be there for comparison ;)

Your labelNode may not be inside the SKScene anymore. This does not mean that it will not respond to the containsPoint function. The labelNode still has a position assigned to it and it can calculate if a point falls inside it using containsPoint function.

Instead you can try this.

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

    let touch = touches.anyObject() as UITouch
    let location = touch.locationInNode(self)

    if self.nodeAtPoint(location) === lemonadeLabel {

        println("lemonadeLabel pressed")
        lemonadeLabel.removeFromParent()

    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!