我认为您正在寻找的是enumerateChildNodesWithName
. 当它被调用时,它将在每个节点上运行它包含的代码,其中包含您输入的名称。
bubble.name = "bubble"
如果将气泡命名为“气泡”,则其所有子节点也将命名为“气泡”。
enumerateChildNodesWithName("bubble"){node, stop in
let bubbleChild:SKSpriteNode = node as! SKSpriteNode
//Run the code telling them what to do here. You should make it so that
//it can figure out how to move the node a slight amount based on its position
}
您可以通过将它放在由计时器调用的函数中来调用它,并且应该经常调用计时器,例如每秒0.017
(大约每秒 60 次),以便运动平稳。例如:
var timer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(0.017), target: self, selector: Selector("moveBubbles:"), userInfo: nil, repeats: true)
然后,创建一个计时器调用的函数:
func moveBubbles(timer: NSTimer!){
//enumerateChildNodesWithName here
}
这里有更多信息,enumerateChildNodesWithName
这里有更多信息。NSTimer