[英]Swift + Sprite Kit Touch Detection
I am just wondering how to remove an SKSprite Node from the scene. 我只是想知道如何从场景中删除一个SKSprite节点。 This is what I have so far:
这是我到目前为止的内容:
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()
}
}
}
}
I am creating lots of these monsters on the screen but when I tap on one of them it doesn't do anything. 我正在屏幕上创建许多这些怪物,但是当我点击其中一个时,它什么也没做。 If I trying adding
println("touched")
it tells me that it has been touched. 如果我尝试添加
println("touched")
它会告诉我它已被触摸。
When you do monster.removeFromParent()
this does not remove the touched node because monster
is not a reference to the touched node. 当您执行
monster.removeFromParent()
这不会删除触摸的节点,因为monster
不是对触摸节点的引用。 To remove the touched node you can use the following code: 要删除触摸的节点,可以使用以下代码:
for touch in touches {
let location = (touch as UITouch).locationInNode(self)
if let theMonster = self.nodeAtPoint(location)
if theMonster.name == "monster" {
theMonster.removeFromParent()
}
}
}
Are you keeping track of your monsters? 您是否在追踪怪物? If not please keep track of those by adding these to a Mutable Array.
如果不是,请通过将它们添加到可变数组来跟踪它们。 Also add unique name to each sprite.
还要为每个精灵添加唯一的名称。
Then just compare the object with your array and remove that object. 然后,将对象与数组进行比较,然后删除该对象。 Hope this helps.. :)
希望这可以帮助.. :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.