Swift version: 5.10
One powerful and under-used feature of SpriteKit is its ability to recolor SKSpriteNodes
dynamically. This has almost zero performance impact, which makes it perfect for having multiple-colored enemies or players. It can also be animated, meaning that you could for example make your player flash white briefly when they've been hit by an enemy.
To tint a sprite cyan, use this code:
firework.color = UIColor.cyan
firework.colorBlendFactor = 1
If you want to animate the sprite coloring, you'd use this:
let action = SKAction.colorize(with: UIColor.red, colorBlendFactor: 1, duration: 1)
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 7.0 – see Hacking with Swift tutorial 20
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.