Swift- SKEffectNode takes a while to appear

174 Views Asked by At

So I have a pause button in my game that when you press it, the scene gets paused, and everything but one SKNode (the pause menu) gets blurred out. I'm doing this by creating a SKEffectNode that has a filter, and adding everything but the pause menu to it. It works, but it takes a solid 2 seconds for the blur to appear in the background. The scene pauses as soon as you press the button, but the blur and the pause menu only appear a few seconds later. Any ideas?

Here's the code:

     override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

         for touch in (touches as! Set<UITouch>) {
            let location = touch.locationInNode(self)

            if (self.nodeAtPoint(location).name == "PauseButton"){
                if(!scene!.paused) {
                    blurScene()

                    scene!.paused = true
                    self.addChild(pauseMenu!)
                }else {
                    removeBlur()

                    scene!.paused = false
                    pauseMenu!.removeFromParent()
                }
            }
        }
    }

    func blurScene() {
        blurNode = SKEffectNode() //Created in the beginning of the class
        let blur = CIFilter(name: "CIGaussianBlur",    withInputParameters: ["inputRadius": 15.0])
        blurNode!.filter = blur
        self.shouldEnableEffects = true

        for node in self.children {
            node.removeFromParent()
            blurNode!.addChild(node as! SKNode)
        }

        self.addChild(blurNode!)
    }

    func removeBlur() {
        var blurredNodes = [SKNode]()

        for node in blurNode!.children {
            blurredNodes.append(node as! SKNode)
            node.removeFromParent()
        }

        for node in blurredNodes {
            self.addChild(node as SKNode)
        }

        self.shouldEnableEffects = false
        blurNode!.removeFromParent()
    }
1

There are 1 best solutions below

0
On

Try adding the SKEffectNode as root view and add the child nodes to it. Then you can set the blur filter already but

self.shouldEnableEffects = false

when you want to blur simply

self.shouldEnableEffects = true