add button on-off to settingsScene

This commit is contained in:
127-Z3R0 2020-05-10 20:08:05 +02:00
parent c143179df8
commit b30abc4292
3 changed files with 34 additions and 11 deletions

View File

@ -11,10 +11,8 @@ import SpriteKit
class MenuScene: SKScene { class MenuScene: SKScene {
var entityManager: EntityManager! var entityManager: EntityManager!
var soundManager: SoundManager!
override func sceneDidLoad() { override func sceneDidLoad() {
soundManager = SoundManager()
entityManager = EntityManager(scene: self) entityManager = EntityManager(scene: self)
let midX = self.size.width / 2 let midX = self.size.width / 2
let midY = self.size.height / 2 let midY = self.size.height / 2
@ -25,7 +23,7 @@ class MenuScene: SKScene {
onButtonPress: { onButtonPress: {
if CommandLine.arguments.contains("--no-matchmaking") { if CommandLine.arguments.contains("--no-matchmaking") {
self.loadScene(scene: GameScene(size: self.size)) self.loadScene(scene: GameScene(size: self.size))
self.soundManager.stopMenuMusic() SoundManager.stopMenuMusic()
} else { } else {
MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self)
} }
@ -39,8 +37,8 @@ class MenuScene: SKScene {
})) }))
entityManager.add(Background(size: self.size)) entityManager.add(Background(size: self.size))
if SoundManager.isMusicPlaying == false { if SoundManager.isMusicPlaying == false && SoundManager.isMusicEnabled == true {
soundManager.startMenuMusic() SoundManager.startMenuMusic()
} }
} }

View File

@ -14,7 +14,7 @@ class SettingsScene: SKScene {
override func sceneDidLoad() { override func sceneDidLoad() {
entityManager = EntityManager(scene: self) entityManager = EntityManager(scene: self)
let positionX = self.size.width * 0.1 let positionX = self.size.width * 0.1
let positionY = self.size.height * 0.1 let positionY = self.size.height * 0.05
entityManager.add(Button(name: "backToMenuScene", entityManager.add(Button(name: "backToMenuScene",
iconName: "", iconName: "",
@ -23,6 +23,20 @@ class SettingsScene: SKScene {
onButtonPress: { onButtonPress: {
self.loadScene(scene: MenuScene(size: self.size)) self.loadScene(scene: MenuScene(size: self.size))
})) }))
entityManager.add(Button(name: "StopMenuMusic",
iconName: "",
text: "ON/OFF",
position: CGPoint(x: self.size.width / 2, y: self.size.height / 2),
onButtonPress: {
if SoundManager.isMusicPlaying == true {
SoundManager.stopMenuMusic()
SoundManager.isMusicEnabled = false
} else {
SoundManager.isMusicEnabled = true
SoundManager.startMenuMusic()
}
}))
entityManager.add(Background(size: self.size)) entityManager.add(Background(size: self.size))
} }

View File

@ -10,11 +10,12 @@ import SpriteKit
import AVFoundation import AVFoundation
class SoundManager { class SoundManager {
var audioPlayer = AVAudioPlayer() static var audioPlayer = AVAudioPlayer()
var backgroundMainMenuAudio: URL? static var backgroundMainMenuAudio: URL?
static var isMusicPlaying: Bool = false static var isMusicPlaying: Bool = false
static var isMusicEnabled: Bool = true
func startMenuMusic() { static func startMenuMusic() {
SoundManager.isMusicPlaying = true SoundManager.isMusicPlaying = true
backgroundMainMenuAudio = Bundle.main.url(forResource: "intro-music", withExtension: "mp3") backgroundMainMenuAudio = Bundle.main.url(forResource: "intro-music", withExtension: "mp3")
do { do {
@ -24,11 +25,21 @@ class SoundManager {
} }
audioPlayer.numberOfLoops = -1 audioPlayer.numberOfLoops = -1
audioPlayer.prepareToPlay() audioPlayer.prepareToPlay()
audioPlayer.play() if SoundManager.isMusicEnabled == true {
audioPlayer.play()
}
} }
func stopMenuMusic() { static func stopMenuMusic() {
audioPlayer.pause() audioPlayer.pause()
SoundManager.isMusicPlaying = false SoundManager.isMusicPlaying = false
} }
static func setVolume(_ volume: Float) {
audioPlayer.volume = volume
}
static func getVolume() {
return audioPlayer.volume
}
} }