我希望能够根据分数来开启和关闭我的SKEmitterNode(雨粒子)。但是我的update函数会被不断地调用,即使我当前的代码如下,屏幕上也会出现数百万个粒子...我该如何构造我的代码,以便在达到分数时只调用一次雨粒子?
class GameScene: SKScene, SKPhysicsContactDelegate {
func setUpRain() {
if let rainParticle = SKEmitterNode(fileNamed: "Rain") {
rainParticle.position = CGPointMake(frame.size.width, frame.size.height)
rainParticle.name = "rainParticle"
rainParticle.zPosition = Layer.Flash.rawValue
worldNode.addChild(rainParticle)
}
}
func makeItRain() {
let startRaining = SKAction.runBlock {
self.setUpRain()
}
runAction(startRaining, withKey: "Rain")
}
func stopRaining() {
removeActionForKey("Rain")
worldNode.enumerateChildNodesWithName("rainParticle", usingBlock: { node, stop in
node.removeFromParent()
})
}
}
class PlayingState: GKState {
unowned let scene: GameScene //used to gain access to our scene
override func updateWithDeltaTime(seconds: NSTimeInterval) {
scene.updateForegroundAndBackground()
scene.updateScore()
if scene.score > 2 {
scene.makeItRain()
}
if scene.score > 4 {
scene.stopRaining()
}
}
SKAction
序列,它添加一个发射器,等待它完成发射,然后将其从父级中移除...类似于这样:https://dev59.com/ZI3da4cB1Zd3GeqP15XN#31731439 如果您想知道哪些SKEmitterNode
的属性与设置所有这些相关,请告诉我。 - Whirlwind