我只是想知道如何从现场删除一个SKSprite节点。到目前为止,这就是我所拥有的:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = (touch as UITouch).locationInNode(self)
if let theName = self.nodeAtPoint(location).name {
if theName == "monster" {
monster! .removeFromParent()
}
}
}
}我在屏幕上创造了很多这样的怪物,但当我点击其中的一个,它什么也不做。如果我尝试添加println("touched"),它会告诉我它已经被触摸了。
发布于 2015-01-07 16:55:12
当您执行monster.removeFromParent()时,这不会删除已触摸的节点,因为monster不是对触摸节点的引用。要删除已触摸的节点,可以使用以下代码:
for touch in touches {
let location = (touch as UITouch).locationInNode(self)
if let theMonster = self.nodeAtPoint(location)
if theMonster.name == "monster" {
theMonster.removeFromParent()
}
}
}发布于 2015-01-07 04:25:29
你在跟踪你的怪物吗?如果没有,请通过将这些添加到可变数组中来跟踪它们。还为每个雪碧添加唯一的名称。
然后,将对象与数组进行比较,然后删除该对象。希望这有帮助..。:)
https://stackoverflow.com/questions/27809376
复制相似问题