gpt4 book ai didi

ios - Swift + Sprite 套件触摸检测

转载 作者:可可西里 更新时间:2023-11-01 01:06:28 25 4
gpt4 key购买 nike

我只是想知道如何从场景中移除 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"),它会告诉我它已被触摸。

最佳答案

当您执行 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()
}
}
}

关于ios - Swift + Sprite 套件触摸检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27809376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com