forked from anitab-org/powerup-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SKTutorialScene.swift
55 lines (42 loc) · 1.77 KB
/
SKTutorialScene.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
import SpriteKit
/** This class is for the tutorial scenes of the mini games. It presents a sequence of tutorial images. When the user taps on each image, the next image will be shown. It will remove itself from the parent node after the last tutorial image, and run the start game code. */
class SKTutorialScene: SKSpriteNode {
// MARK: Properties
// The tutorial image sequence.
var imageSequence: [SKTexture]
var currIndex = 0
// This closure will automatically be run after the last image.
var startGameFunc: () -> Void
// MARK: Functions
func nextImage() {
currIndex += 1
// Update the texture (image).
texture = imageSequence[currIndex]
}
// MARK: Constructors
init(namedImages: [String], size: CGSize, startGameFunc: @escaping () -> Void) {
// Initialize the image sequence.
imageSequence = [SKTexture]()
for namedImage in namedImages {
imageSequence.append(SKTexture(imageNamed: namedImage))
}
self.startGameFunc = startGameFunc
super.init(texture: imageSequence.first, color: UIColor.clear, size: size)
// touchesBegan(_:with:) would be called if only isUserInteractionEnabled is true.
self.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) not implemented.")
}
// MARK: Touch Input
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if currIndex < imageSequence.count - 1 {
nextImage()
} else {
// The last image.
removeFromParent()
// Run the start game closure.
startGameFunc()
}
}
}