From c143179df8de7bc1390c6c707a4966757fea8eaf Mon Sep 17 00:00:00 2001 From: 127-Z3R0 <81heti1bif@hft-stuttgart.de> Date: Sun, 10 May 2020 18:47:42 +0200 Subject: [PATCH] fix multiple calls from startMenuMusic with static Bool, stop music when enter Game --- GoldWars/GoldWars/Scenes/MenuScene.swift | 4 ++-- GoldWars/GoldWars/SettingsScene.swift | 1 - GoldWars/GoldWars/SoundManager.swift | 6 +++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index c1b93de..a65bda9 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -25,6 +25,7 @@ class MenuScene: SKScene { onButtonPress: { if CommandLine.arguments.contains("--no-matchmaking") { self.loadScene(scene: GameScene(size: self.size)) + self.soundManager.stopMenuMusic() } else { MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) } @@ -34,12 +35,11 @@ class MenuScene: SKScene { text: "Settings", position: CGPoint(x: midX, y: midY - 80 ), onButtonPress: { - //TODO: create Settings Scene self.loadScene(scene: SettingsScene(size: self.size)) })) entityManager.add(Background(size: self.size)) - if soundManager.isMusicPlaying == false { + if SoundManager.isMusicPlaying == false { soundManager.startMenuMusic() } } diff --git a/GoldWars/GoldWars/SettingsScene.swift b/GoldWars/GoldWars/SettingsScene.swift index 99f741b..96aec88 100644 --- a/GoldWars/GoldWars/SettingsScene.swift +++ b/GoldWars/GoldWars/SettingsScene.swift @@ -9,7 +9,6 @@ import SpriteKit class SettingsScene: SKScene { - var entityManager: EntityManager! override func sceneDidLoad() { diff --git a/GoldWars/GoldWars/SoundManager.swift b/GoldWars/GoldWars/SoundManager.swift index 808c321..ec09e1c 100644 --- a/GoldWars/GoldWars/SoundManager.swift +++ b/GoldWars/GoldWars/SoundManager.swift @@ -12,9 +12,10 @@ import AVFoundation class SoundManager { var audioPlayer = AVAudioPlayer() var backgroundMainMenuAudio: URL? - var isMusicPlaying: Bool = false + static var isMusicPlaying: Bool = false func startMenuMusic() { + SoundManager.isMusicPlaying = true backgroundMainMenuAudio = Bundle.main.url(forResource: "intro-music", withExtension: "mp3") do { audioPlayer = try AVAudioPlayer(contentsOf: backgroundMainMenuAudio!) @@ -24,11 +25,10 @@ class SoundManager { audioPlayer.numberOfLoops = -1 audioPlayer.prepareToPlay() audioPlayer.play() - isMusicPlaying = true } func stopMenuMusic() { audioPlayer.pause() - isMusicPlaying = false + SoundManager.isMusicPlaying = false } }