当我暂停游戏时,我试图模糊一个场景并且我跟随example,但我无法在Swift 2.0中解决这个问题。
许多教程说只是截取屏幕截图,然后将屏幕截图显示为模糊,但我认为这不是一个好主意,我想在没有屏幕截图的情况下模糊视图。
这是我的尝试:
func createlayers() {
let node = SKEffectNode()
node.shouldEnableEffects = false
let filter: CIFilter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius" : NSNumber(double:1.0)])!
node.filter = filter
}
func blurWithCompletion() {
let duration: CGFloat = 0.5
scene!.shouldRasterize = true
scene!.shouldEnableEffects = true
scene!.runAction(SKAction.customActionWithDuration(0.5, actionBlock: { (node: SKNode, elapsedTime: CGFloat) in
let radius = (elapsedTime/duration)*10.0
(node as? SKEffectNode)!.filter!.setValue(radius, forKey: "inputRadius")
}))
}
func pauseGame()
{
self.blurWithCompletion()
self.view!.paused = true
}
我得到了#34;致命错误:在打开一个可选值&#34时意外地发现了nil;
答案 0 :(得分:2)
不需要创建图层方法。 使用此更新的blurWithCompletion方法:
func blurWithCompletion() {
let duration: CGFloat = 0.5
let filter: CIFilter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius" : NSNumber(double:1.0)])!
scene!.filter = filter
scene!.shouldRasterize = true
scene!.shouldEnableEffects = true
scene!.runAction(SKAction.customActionWithDuration(0.5, actionBlock: { (node: SKNode, elapsedTime: CGFloat) in
let radius = (elapsedTime/duration)*10.0
(node as? SKEffectNode)!.filter!.setValue(radius, forKey: "inputRadius")
}))
}