-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParallaxBackgroundMover.swift
54 lines (40 loc) · 1.89 KB
/
ParallaxBackgroundMover.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
//
// CameraNodeParallax.swift
//
// Created by gitmalong
//
//
import Foundation
import SpriteKit
/*
Gives you parallax backgrounds by moving your bgs by factor x,y in relation to your SKCameraNodes speed
Usage:
1) Create an instance of this class in your scene and call this objects
triggerChange() once your camera position changes
the second vector parameter defines the moving speed of your background in relation to the cameras speed (it gets multiplied with the cameras position difference once your camera changes its position)
e.g. if your background should
- stick to your camera: CGVectorMake(1,1)
- move half as fast as the camera on the x-axes: CGVectorMake(0.5,0)
- not move at all: CGVectorMake(0,0)
- move twice as fast as the camera on x-axes: CGVectorMake(2,0)
*/
public class ParallaxBackgroundMover {
private var backgroundNode:SKNode
private var relativeSpeedToCamera:CGVector // in relation to camera nodes speed
public init(background:SKNode, relativeSpeedToCamera:CGVector) {
self.backgroundNode = background
self.relativeSpeedToCamera = relativeSpeedToCamera
}
/* call this method whenever your cameras position changes */
public func triggerChange(newCameraPosition:CGPoint,oldCameraPosition:CGPoint) {
// Move your parallax backgrounds
let positionChangeX = newCameraPosition.x-oldCameraPosition.x
let positionChangeY = newCameraPosition.y-oldCameraPosition.y
let changeX = positionChangeX*relativeSpeedToCamera.dx
let changeY = positionChangeY*relativeSpeedToCamera.dy
backgroundNode.position = CGPointMake(backgroundNode.position.x+changeX,backgroundNode.position.y+changeY)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}