I'm trying to blur a SKShapenode in my program. However, I want the shapenode to blur progressively, with a duration of about 1 second. How do I achieve that? The current code blurs it instantly.
func generateAnimation() {
var blurAction : SKAction = SKAction.runBlock{
//the method below returns a shapeNode
var circle = self.generateImage()
var effect : SKEffectNode = SKEffectNode()
var filter : CIFilter = CIFilter(name:"CIGaussianBlur")
filter.setValue(10, forKey: "inputRadius")
effect.filter = filter
effect.addChild(circle)
self.addChild(effect)
}
Implement the update
method in your SKScene
subclass (or delegate). Then, over the course of one second, run this line again each time your update
method is called:
filter.setValue(10, forKey: "inputRadius")
Except instead of passing the value 10
, interpolate between 0 and 10 based on the elapsed time.
You might find that re-rendering a blur every frame makes it hard to keep a smooth frame rate. So you might consider faking it instead. Make two nodes, one of which has the blur effect, and use fadeInWithDuration
/fadeOutWithDuration
actions to fade in the blurred node and fade out the unblurred node.
I agree with @rickster. In the apple spritekit demo they used this to simulate blinking of the lights (in which the lights fade out from the screen --> "blur" the light).
- (SKSpriteNode *)newLight
{
SKSpriteNode *light = [[SKSpriteNode alloc] initWithColor:[SKColor yellowColor] size:CGSizeMake(8,8)];
SKAction *blink = [SKAction sequence:@[
[SKAction fadeOutWithDuration:0.25],
[SKAction fadeInWithDuration:0.25]]];
SKAction *blinkForever = [SKAction repeatActionForever:blink];
[light runAction: blinkForever];
return light;
}
You can modify the behaviour of "blinkForever" to suit your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With