forked from jeffreysfllo24/NightBall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuScene.swift
197 lines (153 loc) · 7.64 KB
/
MenuScene.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// MenuScene.swift
// NightBallGame
//
// Created by Jeffrey Zhang on 2017-08-30.
// Copyright © 2017 Keener Studio. All rights reserved.
//
import SpriteKit
import AVFoundation
import GameplayKit
class MenuScene: SKScene {
// Music
var AudioPlayer3 = AVAudioPlayer()
// Music for Gamescene
var AudioPlayer1 = AVAudioPlayer()
var AudioPlayer2 = AVAudioPlayer()
var AudioPlayer4 = AVAudioPlayer()
// Play button image
var playButton = SKSpriteNode()
let playButtonTex = SKTexture(imageNamed: "Playbutton")
let Menubackground: SKSpriteNode = SKSpriteNode(imageNamed: "menubackground")
var SoundIcon = SKSpriteNode()
let SoundIconTex = SKTexture(imageNamed: "SoundIcon")
var Soundmute = SKSpriteNode()
let SoundmuteTex = SKTexture (imageNamed: "Soundmute")
let title: SKSpriteNode = SKSpriteNode(imageNamed: "AppTitle-5")
let fade :SKSpriteNode = SKSpriteNode(imageNamed: "StarBackground1")
let fade2 :SKSpriteNode = SKSpriteNode(imageNamed: "StarBackground2")
let fade3 :SKSpriteNode = SKSpriteNode(imageNamed: "StarBackground3")
let fade4 :SKSpriteNode = SKSpriteNode(imageNamed: "StarBackground4")
override func didMove(to view: SKView) {
// Add Background
Menubackground.position = CGPoint(x: size.width * 0.5, y: size.height * 0.5)
Menubackground.size = self.frame.size;
Menubackground.zPosition = -6
addChild(Menubackground)
// Insert Title
title.position = CGPoint(x: size.width * 0.5, y: size.height * 0.85)
title.scale(to: CGSize(width: size.width * 0.6, height: size.height * 0.13))
title.zPosition = 1
addChild(title)
// Insert Play button
playButton = SKSpriteNode(texture: playButtonTex)
playButton.position = CGPoint(x: frame.midX, y: frame.midY)
playButton.scale(to: CGSize(width: size.width * 0.6, height: size.width * 0.6))
playButton.zPosition = 4
self.addChild(playButton)
let ismuted = appDelegate.ismuted
if ismuted! {
// Add Muted Icon
Soundmute = SKSpriteNode(texture: SoundmuteTex)
Soundmute.position = CGPoint(x: size.width * 0.5, y: size.height * 0.2)
Soundmute.scale(to: CGSize(width: size.width * 0.15, height: size.height * 0.06))
Soundmute.zPosition = 4
self.addChild(Soundmute)
} else {
// Add Sound Icon
SoundIcon = SKSpriteNode(texture: SoundIconTex)
SoundIcon.position = CGPoint(x: size.width * 0.5, y: size.height * 0.2)
SoundIcon.scale(to: CGSize(width: size.width * 0.15, height: size.height * 0.06))
SoundIcon.zPosition = 4
self.addChild(SoundIcon)
}
// First Star background
fade.position = CGPoint(x: size.width * 0.5, y: size.height * 0.4)
fade.scale(to: CGSize(width: 200, height: 600))
fade.zPosition = 1
let waitAction = SKAction.wait(forDuration: 2)
let animateList = SKAction.sequence([waitAction, SKAction.fadeIn(withDuration: 1.7),SKAction.fadeOut(withDuration: 1.7)])
let repeatFade:SKAction = SKAction.repeatForever(animateList)
fade.run(repeatFade)
self.addChild(fade)
// Second Star background
fade2.position = CGPoint(x: size.width * 0.3, y: size.height * 0.4)
fade2.scale(to: CGSize(width: 200, height: 600))
fade2.zPosition = 1
let animateList2 = SKAction.sequence([SKAction.fadeIn(withDuration: 5.7),SKAction.fadeOut(withDuration: 5.7)])
let repeatFade2:SKAction = SKAction.repeatForever(animateList2)
fade2.run(repeatFade2)
self.addChild(fade2)
// Third Star background
fade3.position = CGPoint(x: size.width * 0.7, y: size.height * 0.4)
fade3.scale(to: CGSize(width: 200, height: 600))
fade3.zPosition = 1
let waitAction2 = SKAction.wait(forDuration: 1)
let animateList3 = SKAction.sequence([waitAction2,SKAction.fadeIn(withDuration: 2.6),SKAction.fadeOut(withDuration: 2.6)])
let repeatFade3:SKAction = SKAction.repeatForever(animateList3)
fade3.run(repeatFade3)
self.addChild(fade3)
// Add Music
let AssortedMusics = NSURL(fileURLWithPath: Bundle.main.path(forResource: "Hypnothis", ofType: "mp3")!)
AudioPlayer3 = try! AVAudioPlayer(contentsOf: AssortedMusics as URL)
AudioPlayer3.prepareToPlay()
AudioPlayer3.numberOfLoops = -1
if !ismuted! {
AudioPlayer3.play()
}
}
// access global AppDelegate
let appDelegate = UIApplication.shared.delegate as! AppDelegate
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// If the play button is touched enter game scene
if let touch = touches.first {
let pos = touch.location(in: self)
let node = self.atPoint(pos)
if node == playButton {
if let view = view {
if AudioPlayer3.isPlaying{
let fadeOutAction = SKAction.fadeOut(withDuration: 5)
playButton.run(fadeOutAction)
let transition:SKTransition = SKTransition.crossFade(withDuration: 1)
let scene:SKScene = GameScene(size: self.size,audio: false)
self.view?.presentScene(scene, transition: transition)
}
else{
let fadeOutAction = SKAction.fadeOut(withDuration: 5)
playButton.run(fadeOutAction)
let transition:SKTransition = SKTransition.crossFade(withDuration: 1)
let scene:SKScene = GameScene(size: self.size,audio: true)
self.view?.presentScene(scene, transition: transition)
}
}
}
if node == SoundIcon || node == Soundmute {
// retrieve ismuted bool from global AppDelegate
let ismuted = appDelegate.ismuted
if ismuted! {
// Remove Mute Sound Icon
Soundmute.removeFromParent()
// Replace Mute Sound Icon with Sound Icon
SoundIcon = SKSpriteNode(texture: SoundIconTex)
SoundIcon.position = CGPoint(x: size.width * 0.5, y: size.height * 0.2)
SoundIcon.scale(to: CGSize(width: size.width * 0.15, height: size.height * 0.06))
SoundIcon.zPosition = 4
self.addChild(SoundIcon)
appDelegate.ismuted = false
AudioPlayer3.play()
} else {
//Remove SoundIcon
SoundIcon.removeFromParent()
//Add Mute Sound Icon
Soundmute = SKSpriteNode(texture: SoundmuteTex)
Soundmute.position = CGPoint(x: size.width * 0.5, y: size.height * 0.2)
Soundmute.scale(to: CGSize(width: size.width * 0.15, height: size.height * 0.06))
Soundmute.zPosition = 4
self.addChild(Soundmute)
appDelegate.ismuted = true
AudioPlayer3.pause()
}
}
}
}
}