-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainMenuScene.swift
60 lines (46 loc) · 2.21 KB
/
MainMenuScene.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// MainMenuScene.swift
// Color-Pong
//
// Created by Blake Ehrenbeck on 3/25/17.
// Copyright © 2017 huangzhen. All rights reserved.
//
import UIKit
import SpriteKit
class MainMenuScene: SKScene {
func starfieldEmitter(color: SKColor, starSpeedY: CGFloat, starsPerSecond: CGFloat, starScaleFactor: CGFloat) -> SKEmitterNode {
// Determine the time a star is visible on screen
let lifetime = frame.size.height * UIScreen.main.scale / starSpeedY
// Create the emitter node
let emitterNode = SKEmitterNode()
emitterNode.particleTexture = SKTexture(imageNamed: "Star")
emitterNode.particleBirthRate = starsPerSecond
emitterNode.particleColor = SKColor.lightGray
emitterNode.particleSpeed = starSpeedY * -1
emitterNode.particleScale = starScaleFactor
emitterNode.particleColorBlendFactor = 1
emitterNode.particleLifetime = lifetime
// Position in the middle at top of the screen
emitterNode.position = CGPoint(x: frame.size.width/2, y: frame.size.height)
emitterNode.particlePositionRange = CGVector(dx: frame.size.width, dy: 0)
// Fast forward the effect to start with a filled screen
emitterNode.advanceSimulationTime(TimeInterval(lifetime))
return emitterNode
}
override func didMove(to view: SKView) {
// Add Starfield with 3 emitterNodes for a parallax effect
// - Stars in top layer: light, fast, big
// - ...
// - Stars in back layer: dark, slow, small
var emitterNode = starfieldEmitter(color: SKColor.lightGray, starSpeedY: 50, starsPerSecond: 1, starScaleFactor: 0.2)
emitterNode.zPosition = -10
self.addChild(emitterNode)
emitterNode = starfieldEmitter(color: SKColor.gray, starSpeedY: 30, starsPerSecond: 2, starScaleFactor: 0.1)
emitterNode.zPosition = -11
self.addChild(emitterNode)
emitterNode = starfieldEmitter(color: SKColor.darkGray, starSpeedY: 15, starsPerSecond: 4, starScaleFactor: 0.05)
emitterNode.zPosition = -12
self.addChild(emitterNode)
self.backgroundColor = .black
}
}