fix multiple calls from startMenuMusic with static Bool, stop music when enter Game

This commit is contained in:
127-Z3R0 2020-05-10 18:47:42 +02:00
parent 888d194676
commit c143179df8
3 changed files with 5 additions and 6 deletions

View File

@ -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()
}
}

View File

@ -9,7 +9,6 @@
import SpriteKit
class SettingsScene: SKScene {
var entityManager: EntityManager!
override func sceneDidLoad() {

View File

@ -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
}
}