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

查看:74
本文介绍了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*/

        }

    }


推荐答案

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

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

你基本上确定如果lemonadeLabel是该位置的节点,如果是,则将其删除。由于您与场景中添加的节点进行比较,如果它已经消失,则不会进行比较;)

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天全站免登陆