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

View File

@ -14,7 +14,7 @@ class SettingsScene: SKScene {
override func sceneDidLoad() {
entityManager = EntityManager(scene: self)
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",
iconName: "",
@ -23,6 +23,20 @@ class SettingsScene: SKScene {
onButtonPress: {
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))
}

View File

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