SKLabelNode will disappear but is still clickable

若如初见. 提交于 2019-12-20 04:28:21

问题


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 myLabelNode.removeFromParent() it removes the node from the scene, as it should. The node count drops by 1, and it isn't visible anywhere on the screen. However, when I click the spot where myLabelNode previously was, my program will still call out the function that should only happen when myLabelNode is touched. I also tried combining myLabelNode.removeFromParent() with myLabelNode.hidden = true, but it is still touchable, and calls the function even though it shouldn't. How should I fix this? Is there a different method I should be using? Is this supposed to happen?

Edit:

    let lemonadeLabel = SKLabelNode(fontNamed: "Optima-ExtraBlack")

    override func didMoveToView(view: SKView) {

    lemonadeLabel.text = "Lemonade Stand"
    lemonadeLabel.fontSize = 24
    lemonadeLabel.fontColor = SKColor.yellowColor()
    lemonadeLabel.position = CGPoint(x: size.width/2, y: size.height*0.66)
    lemonadeLabel.zPosition = 2.0
    addChild(lemonadeLabel)

    }


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

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

        if lemonadeLabel.containsPoint(location) {

            println("lemonadeLabel pressed")
            lemonadeLabel.removeFromParent()
            /*lemonadeLabel is now be removed,
            however if I click the area where it 
            used to be, "lemonadeLabel pressed"
            will print to the console*/

        }

    }

回答1:


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




回答2:


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

    }

}


来源:https://stackoverflow.com/questions/28228029/sklabelnode-will-disappear-but-is-still-clickable

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