Last active
December 5, 2017 15:16
-
-
Save laevandus/8e47ae86c25444ca09b5f5199253102c to your computer and use it in GitHub Desktop.
Adding linear and radial gradients to SKScene.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class GameScene: SKScene | |
{ | |
override func didMove(to view: SKView) | |
{ | |
let linearGradientSize = size | |
let linearGradientColors = [UIColor(red: 53.0 / 255.0, green: 92.0 / 255.0, blue: 125.0 / 255.0, alpha: 1.0), | |
UIColor(red: 108.0 / 255.0, green: 91.0 / 255.0, blue: 123.0 / 255.0, alpha: 1.0), | |
UIColor(red: 192.0 / 255.0, green: 108.0 / 255.0, blue: 132.0 / 255.0, alpha: 1.0)] | |
let linearGradientLocations: [CGFloat] = [0, 0.5, 1] | |
let textureCount = 8 | |
let textures = (0..<textureCount).map { (index) -> SKTexture in | |
let angle = 2.0 * CGFloat.pi / CGFloat(textureCount) * CGFloat(index) | |
return SKTexture(linearGradientWithAngle: angle, colors: linearGradientColors, locations: linearGradientLocations, size: linearGradientSize) | |
} | |
let linearGradientNode = SKSpriteNode(texture: textures.first) | |
linearGradientNode.zPosition = 1 | |
addChild(linearGradientNode) | |
let action = SKAction.animate(with: textures, timePerFrame: 0.5) | |
linearGradientNode.run(SKAction.repeatForever(action)) | |
let radialGradientSize = CGSize(width: min(size.width, size.height), height: min(size.width, size.height)) | |
let radialGradientColors = [UIColor.yellow, UIColor.orange] | |
let radialGradientLocations: [CGFloat] = [0, 1] | |
let radialGradientTexture = SKTexture(radialGradientWithColors: radialGradientColors, locations: radialGradientLocations, size: radialGradientSize) | |
let radialGradientNode = SKSpriteNode(texture: radialGradientTexture) | |
radialGradientNode.zPosition = 2 | |
addChild(radialGradientNode) | |
let pulse = SKAction.sequence([SKAction.fadeIn(withDuration: 3.0), SKAction.fadeOut(withDuration: 1.0)]) | |
radialGradientNode.run(SKAction.repeatForever(pulse)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment