Skip to content

Instantly share code, notes, and snippets.

@reccanti
Created January 11, 2020 06:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reccanti/d11c9ab2ec4c8b659438e347080876a3 to your computer and use it in GitHub Desktop.
Save reccanti/d11c9ab2ec4c8b659438e347080876a3 to your computer and use it in GitHub Desktop.
A swift playground that modifies the black and white demo to be more flexible and maintainable
import SpriteKit
import PlaygroundSupport
// Variables to manipulate the demo
let COLORS: [UIColor] = [.white, .cyan, .white, .magenta]
let NUM_SHAPES = 6 // The number of shapes that will appear onscreen
let SPEED = 0.5 // the speed at whic the demo will move
/*
// a linear scaling function
func getScaleFactor(index: Int) -> Double {
return 100.0 / Double(NUM_SHAPES) * Double(index)
}
*/
let totalSize = (pow(Double(NUM_SHAPES), 2) + Double(NUM_SHAPES)) / 2
func getScaleFactor(index: Int) -> Double {
var size = 0
for i in 0 ... index {
size += i
}
return 100.0 * Double(size) / totalSize
}
func getScaleAction(from: Int, to: Int, wait: Int = 0) -> SKAction {
let waitDuration = Double(SPEED) * Double(wait)
let scaleDuration = Double(SPEED) * Double(to - from)
let initialScaleFactor = getScaleFactor(index: from)
let finalScaleFactor = getScaleFactor(index: to)
let wait = SKAction.wait(forDuration: waitDuration)
let initialScale = SKAction.scale(to: CGFloat(initialScaleFactor), duration: 0.0)
let finalScale = SKAction.scale(to: CGFloat(finalScaleFactor), duration: scaleDuration)
return SKAction.sequence([
initialScale,
wait,
finalScale
])
}
func createShape(color: UIColor) -> SKShapeNode {
let shape = SKShapeNode(rect: CGRect(x: -0.005, y: -0.005, width: 0.01, height: 0.01))
shape.lineWidth = 0
shape.fillColor = color
shape.position = CGPoint(x: 0.5, y: 0.5)
return shape
}
let shapes = SKNode()
for i in (0...NUM_SHAPES).reversed() {
// INITIALIZATION
let colorIndex = shapes.children.count % COLORS.count
let color = COLORS[colorIndex]
let shape = createShape(color: color)
let initialScaleFactor = getScaleFactor(index: i)
let initialScale = SKAction.scale(to: CGFloat(initialScaleFactor), duration: 0)
shape.run(initialScale)
// LOOP
let scale = getScaleAction(from: i, to: i + COLORS.count)
shape.run(SKAction.repeatForever(scale))
shapes.addChild(shape)
}
for i in (1...COLORS.count - 1) {
// INITIALIZATION
let colorIndex = shapes.children.count % COLORS.count
let color = COLORS[colorIndex]
let shape = createShape(color: color)
let initialScaleFactor = 0
let initialScale = SKAction.scale(to: CGFloat(initialScaleFactor), duration: 0)
shape.run(initialScale)
// LOOP
let scale = getScaleAction(from: 0, to: COLORS.count - i, wait: i)
shape.run(SKAction.repeatForever(scale))
shapes.addChild(shape)
}
let view = SKView(frame: CGRect(x: 0.0, y: 0.0, width: 1000, height: 1000))
let scene = SKScene()
scene.scaleMode = .aspectFit
scene.backgroundColor = #colorLiteral(red: 0.807843137254902, green: 0.027450980392156862, blue: 0.3333333333333333, alpha: 1.0)
scene.addChild(shapes)
view.presentScene(scene)
PlaygroundPage.current.liveView = view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment