如何模糊除2个节点之外的所有内容Spritekit(Swift)

时间:2014-10-15 14:32:33

标签: iphone xcode swift sprite-kit blur

时,我想模糊游戏的背景
self.view?.scene?.paused = true

但是按钮和暂停的标签(两个SKSpriteNode)都不应该模糊。它们都有不同的Z指数值。 按下按钮节点时暂停场景,再次按下按钮时恢复场景。

我无法在Swift中找到实现这一目标的方法。 我找到了一些使用SKEffectNode的建议?

1 个答案:

答案 0 :(得分:10)

基本步骤......

  1. 创建SKEffectsNode
  2. 创建CIGaussianBlur CIFilter
  3. 将过滤器分配给效果节点
  4. 将节点添加到效果节点(子节点将模糊)
  5. 和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)