Noah Covey
Noah Covey

Reputation: 161

SpriteKit Swift make an entire scene darker, then change it back

Is there any way to make an entire scene, including all of its children nodes (not just the background color) darker in color? I am looking for an effect similar to that of this code:

node.color = SKColor.blackColor()
node.colorBlendFactor = 0.25

The above code shades the node a darker color, while keeping the original colors (except that those colors are just darker) and detail. However, as far as I can tell, this code doesn't work on scenes, it only works on SKSpriteNode. Is there any way to darken a whole scene? The answer could be some sort of filter, a special way of colorizing a scene, or maybe there is just no way. Anything helps!

Thanks

Upvotes: 2

Views: 1566

Answers (2)

rickster
rickster

Reputation: 126167

The answer could be some sort of filter...

Indeed. While @TrentSartain's answer is quite practical, it's not the only way. SKScene inherits from SKEffectNode, so you can apply a Core Image filter (or filter chain) to the entire scene as a way to darken it or add other visual effects.

Once you get into Core Image filters, you have all kinds of options for darkening — you can get a different effect from ramping the parameters to CIExposureAdjust than with CIToneCurve, for example. Or you could combine several filters, so your scene (for example) fades to grayscale and blurs as it darkens.

Note: there are no SKActions for animating Core Image filters, but you can change the parameters of a CIFilter object over time in your update function or a customActionWithDuration:actionBlock: block.

Upvotes: 3

Trent Sartain
Trent Sartain

Reputation: 446

When I need to do something similar to what you are asking, I simply add a SKSpriteNode on top of everything else as follows:

let dimPanel = SKSpriteNode(color: UIColor.blackColor(), size: self.size)
dimPanel.alpha = 0.75
dimPanel.zPosition = 100
dimPanel.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(dimPanel)

Just make sure the panel's zPosition is greater than everything you want to make dimmer.

This way, you can also use the SKAction.colorizeWithColor to change the panel to whatever color you want.

To add the panel and then remove it, you could start the alpha at 0, then do:

dimPanel.runAction(SKAction.sequence([
    SKAction.fadeAlphaTo(0.75, duration: 3),
    SKAction.fadeOutWithDuration(3)
]))

Upvotes: 4

Related Questions