Add new Buttons to SettingsScene
* Due to UserDefaults change label by start * add logging in SoundManager finish work
This commit is contained in:
parent
2f664712d0
commit
6970f27a61
@ -6,4 +6,24 @@
|
||||
// Copyright © 2020 SP2. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import GameKit
|
||||
|
||||
class SettingsButton: GKEntity {
|
||||
|
||||
var name: String
|
||||
var settingsButton: ButtonNode
|
||||
|
||||
init(name: String, text: String, position: CGPoint, onButtonPress: @escaping () -> ()) {
|
||||
self.name = name
|
||||
settingsButton = ButtonNode(textureName: "yellow_button04",
|
||||
text: text,
|
||||
isEnabled: true,
|
||||
position: position,
|
||||
onButtonPress: onButtonPress)
|
||||
super.init()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
}
|
||||
|
@ -11,11 +11,14 @@ import SpriteKit
|
||||
class SettingsScene: SKScene {
|
||||
|
||||
var entityManager = EntityManager.settingsEMInstance
|
||||
var musicButtonText = ""
|
||||
var isMusicDeactivatedByUserDefault = UserDefaults.standard.bool(forKey: "noMusic")
|
||||
|
||||
override func sceneDidLoad() {
|
||||
entityManager.setScene(scene: self)
|
||||
let positionX = self.size.width * 0.1
|
||||
let positionY = self.size.height * 0.05
|
||||
isMusicDeactivatedByUserDefault ? setMusicButtonTextByUserDefault(text: "OFF") : setMusicButtonTextByUserDefault(text: "ON")
|
||||
entityManager.add(Button(name: "backToMenuScene",
|
||||
textureName: "yellow_button04",
|
||||
text: "Back",
|
||||
@ -24,28 +27,30 @@ class SettingsScene: SKScene {
|
||||
let scene = MenuScene(size: self.size)
|
||||
self.loadScene(scene: scene)
|
||||
}))
|
||||
entityManager.add(Button(name: "StopMenuMusic",
|
||||
textureName: "yellow_button04",
|
||||
text: "ON/OFF",
|
||||
entityManager.add(SettingsButton(name: "StopMenuMusic",
|
||||
text: musicButtonText,
|
||||
position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2),
|
||||
onButtonPress: {
|
||||
if SoundManager.sharedInstance.isMusicPlaying {
|
||||
SoundManager.sharedInstance.stopMenuMusic()
|
||||
SoundManager.sharedInstance.isMusicEnabled = false
|
||||
self.entityManager.changeSettingsButtonText(buttonName: "StopMenuMusic", text: "OFF")
|
||||
} else {
|
||||
SoundManager.sharedInstance.isMusicEnabled = true
|
||||
SoundManager.sharedInstance.startMenuMusic()
|
||||
self.entityManager.changeSettingsButtonText(buttonName: "StopMenuMusic", text: "ON")
|
||||
}
|
||||
}))
|
||||
entityManager.add(Button(name: "StopMovingBackground",
|
||||
textureName: "yellow_button04",
|
||||
text: "MOVE/STOP",
|
||||
entityManager.add(SettingsButton(name: "StopMovingBackground",
|
||||
text: "MOVE",
|
||||
position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2 - 100),
|
||||
onButtonPress: {
|
||||
if BackgroundComponent.isMovingBackgroundEnabled {
|
||||
BackgroundComponent.isMovingBackgroundEnabled = false
|
||||
self.entityManager.changeSettingsButtonText(buttonName: "StopMovingBackground", text: "STOP")
|
||||
} else {
|
||||
BackgroundComponent.isMovingBackgroundEnabled = true
|
||||
self.entityManager.changeSettingsButtonText(buttonName: "StopMovingBackground", text: "MOVE")
|
||||
}
|
||||
}))
|
||||
entityManager.add(Label(fontnamed: "Courier-Bold",
|
||||
@ -83,6 +88,10 @@ class SettingsScene: SKScene {
|
||||
entityManager.add(Background(size: self.size))
|
||||
}
|
||||
|
||||
func setMusicButtonTextByUserDefault(text: String) {
|
||||
self.musicButtonText = text
|
||||
}
|
||||
|
||||
func loadScene(scene: SKScene) {
|
||||
let transition = SKTransition.flipVertical(withDuration: 0.5)
|
||||
entityManager.entities.removeAll()
|
||||
|
@ -27,7 +27,7 @@ class SoundManager {
|
||||
do {
|
||||
audioPlayer = try AVAudioPlayer(contentsOf: backgroundMainMenuAudio!)
|
||||
} catch {
|
||||
//TODO: Add logging
|
||||
os_log("backgroundMusic is broken", log: LOG, type: .error)
|
||||
}
|
||||
audioPlayer.numberOfLoops = -1
|
||||
audioPlayer.prepareToPlay()
|
||||
|
Loading…
Reference in New Issue
Block a user