【问题标题】:How to switch on score to create an SKEmitterNode using SpriteKit?如何使用 SpriteKit 打开分数以创建 SKEmitterNode?
【发布时间】: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


【解决方案1】:

有几种方法可以做到这一点,但其中最简单的方法是每次切换只调用一次 makeItRain() 或 stopRaining()。我的意思是一旦调用了 makeItRain,在调用 stopRaining 之前不能再次调用它。这可以使用如下的布尔值来完成:

   var rainToggle: Bool = false; //True = Raining
override func updateWithDeltaTime(seconds: NSTimeInterval) {
                scene.updateForegroundAndBackground()
                scene.updateScore()

                if (scene.score > 4){
                    scene.stopRaining()
                    rainToggle = false;
                }
                else if (scene.score > 2 && !rainToggle) {
                    scene.makeItRain()
                    rainToggle = true;

                }
            }

这只是稍微低效,因为您无缘无故地在每一帧调用 stopRaining(),但是它完成了工作并且很容易理解。另请注意,我必须颠倒您的 if 语句的顺序(否则它将不起作用)。

【讨论】:

  • 谢谢!我在想解决方案是使用 bool,但我不太明白。
猜你喜欢
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-17
  • 2014-03-23
  • 1970-01-01
相关资源
最近更新 更多