SKLabelNode will disappear but is still clickable

后端 未结 2 1137
傲寒
傲寒 2021-01-24 07:58

I am making a game using SpriteKit and Swift, running Xcode 6. I have an SKLabelNode, let\'s call it myLabelNode for this example. When I call my

相关标签:
2条回答
  • 2021-01-24 08:40

    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()
    
        }
    
    }
    
    0 讨论(0)
  • 2021-01-24 08:49

    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 ;)

    0 讨论(0)
提交回复
热议问题