SKLabelNode 将消失但仍可点击 [英] SKLabelNode will disappear but is still clickable

查看:18
本文介绍了SKLabelNode 将消失但仍可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SpriteKit 和 Swift 制作游戏,运行 Xcode 6.我有一个 SKLabelNode,我们将其称为 myLabelNode.当我调用 myLabelNode.removeFromParent() 时,它应该从场景中删除节点.节点数减少 1,并且在屏幕上的任何位置都不可见.但是,当我单击 myLabelNode 之前所在的位置时,我的程序仍会调用仅在触摸 myLabelNode 时才会发生的函数.我还尝试将 myLabelNode.removeFromParent()myLabelNode.hidden = true 结合使用,但它仍然是可触摸的,并且即使它不应该调用该函数.我应该如何解决这个问题?我应该使用其他方法吗?这应该发生吗?

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?

    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*/

        }

    }

推荐答案

您正在尝试确定约束点的位置是否被触摸.即使您从场景中删除标签,它仍然是内存中的一个对象,即:您可以稍后重新添加它.它仍然具有它的所有属性,包括位置等.

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..

我会试试这个:

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

这篇关于SKLabelNode 将消失但仍可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆