【发布时间】:2016-09-18 05:34:07
【问题描述】:
我希望能够根据分数打开和关闭我的 SKEmitterNode(雨粒子)。但是我的更新函数会不断被调用,即我最终在屏幕上显示了数百万个粒子,而我的当前代码在下面……我该如何构造我的代码,以便在达到分数时只调用一次雨粒子?
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序列,它添加一个发射器,等待它完成发射,然后将其从父级中删除......像这样:stackoverflow.com/a/31731439/3402095 如果您有兴趣SKEmitterNode的哪些属性与设置所有这些相关,让我知道。 -
非常感谢!我想我遵循了,但是发射器(1)出生率和(2)最大和寿命(1)开始和(2)范围应该是什么类型的值,以便产生短暂的阵雨效果,然后停止?
-
我看到你已经知道哪些属性是相关的,但是如何下雨,嗯,这是你的工作,你必须玩弄它。 :)
-
会的,谢谢! :)
标签: ios swift sprite-kit