0

所以我想制作一个代码块,每帧都会在精灵节点内运行。结果是我点击并在那里创建一个气泡,然后使用正弦向上浮动。这有可能在泡沫中编码吗?抱歉,如果这看起来含糊不清,我只是不知道该怎么称呼它。我能想到的最接近的事情是克隆,每个克隆每帧都运行相同的脚本。无论如何,这就是我现在所拥有的。

let bubble = SKSpriteNode(imageNamed:"bubble")    
bubble.position.x = CGFloat(rand())*self.frame.width
bubble.position.y = -20

//Code for bubble to run each frame

self.addChild(bubble)
4

1 回答 1

0

我认为您正在寻找的是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

于 2015-11-23T14:16:47.557 回答