How to blur everything except 2 nodes. Spritekit (Swift)

末鹿安然 提交于 2019-12-03 17:33:20

问题


I'd like to blur the background of my game when

self.view?.scene?.paused = true

But the the button and the paused label (both SKSpriteNode's) should not be blur. they all have different Z-index values. The scene is paused when the button node is pressed and resumed when the button is pressed again.

I cant find a way to achieve this in Swift. i found some suggestions that use SKEffectNode?


回答1:


The basic steps...

  1. Create an SKEffectsNode
  2. Create a CIGaussianBlur CIFilter
  3. Assign the filter to the effects node
  4. Add nodes to the effects node (child nodes will be blurred)

and example code in Swift...

// Create an effects node with a gaussian blur filter
let effectsNode = SKEffectNode()
let filter = CIFilter(name: "CIGaussianBlur")
// Set the blur amount. Adjust this to achieve the desired effect
let blurAmount = 10.0
filter?.setValue(blurAmount, forKey: kCIInputRadiusKey)

effectsNode.filter = filter
effectsNode.position = self.view!.center
effectsNode.blendMode = .alpha

// Create a sprite
let texture = SKTexture(imageNamed: "Spaceship")
let sprite = SKSpriteNode(texture: texture)

// Add the sprite to the effects node. Nodes added to the effects node
// will be blurred
effectsNode.addChild(sprite)
// Add the effects node to the scene
self.addChild(effectsNode)

// Create another sprite
let sprite2 = SKSpriteNode(texture: texture)
sprite2.position = self.view!.center
sprite2.size = CGSize(width:64, height:64);
sprite2.zPosition = 100

// Add the sprite to the scene. Nodes added to the scene won't be blurred
self.addChild(sprite2)


来源:https://stackoverflow.com/questions/26385156/how-to-blur-everything-except-2-nodes-spritekit-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!