2017-03-09 63 views
2

如何在我的设置中制作“激活/关闭背景音乐”按钮?Swift:根据游戏状态,如何制作UISwitch按钮作为设置?

音乐只在游戏启动时播放。 (点击“开始”播放 - >背景音乐也播放。)

我已经编码了这一刻,但我真的不知道该怎么做。

class SettingsViewController: UITableViewController { 
    // MARK: - IBOutlets 
    @IBOutlet weak var backgroundgMusicLabel: UILabel! 
    @IBOutlet weak var backgroundMusicSwitch: UISwitch! 

    // MARK: - Lifecycle 
    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    @IBAction func backgroundMusicPressed(_ sender: Any) { 
     if backgroundMusicSwitch.isOn { 
      UserDefaults().set("On", forKey: "switch") 
     } else { 
      UserDefaults().set("Off", forKey: "switch") 
     } 
    } 
} 

SettingsViewController

虽然我有这个代码在我MainViewController:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if game == .ready { 
     start() 
    } 
} 

和我一起玩音乐是这样的:

var audioPlayer: AVAudioPlayer? 

func playMusic() { 
    let url = Bundle.main.url(forResource: "MyMusic", withExtension: "mp3")! 
    do { 
     audioPlayer = try AVAudioPlayer(contentsOf: url) 
     guard let audioPlayer = audioPlayer else { return } 
     audioPlayer.prepareToPlay() 
     audioPlayer.play() 
    } catch let error { 
     print(error.localizedDescription) 
    } 
} 

回答

2

TheValyreanGroup与Globals合适。

class Globals: NSObject { 
    static let sharedInstance = Globals() 
    var audioPlayer: AVAudioPlayer? 

    func playMusic() { 
     let url = Bundle.main.url(forResource: "MyMusic", withExtension: "mp3")! 
     do { 
      audioPlayer = try AVAudioPlayer(contentsOf: url) 
      guard let audioPlayer = audioPlayer else { return } 
      audioPlayer.prepareToPlay() 
      audioPlayer.play() 
     } catch let error { 
      print(error.localizedDescription) 
     } 
    } 
} 

但在你的情况下,尝试下面的代码:

MainViewController:

static let sharedInstance = Globals() 
let globalVars = Globals.sharedInstance 

而在你开始添加此FUNC()功能:

func checkMusicStatus() { 
    let status = UserDefaults().string(forKey: "switch") 
    if status == "Off"{ 
     if (self.globalVars.audioPlayer?.isPlaying != nil){ 
      self.globalVars.audioPlayer?.stop() 
     } 
    } else { 
     self.globalVars.playMusic() 
    } 
} 

它应该工作,希望这会有所帮助。

0

当我需要做的事情像这样我喜欢使用SharedInstance类。一些人称它为单身,但我不相信它实际上是。

创建一个新的swift文件并将其命名为'Globals'。声明你想要在VC之间共享的任何变量或对象,你也可以创建全局函数。请记住,这些都将是同一个例子。这与在VC之间传递变量或者获取另一个VC的另一个实例不同。

Globals.swift

class Globals: NSObject { 

    static let sharedInstance = Globals() 

    var audioPlayer: AVAudioPlayer? 

    func playMusic() { 
     let url = Bundle.main.url(forResource: "MyMusic", withExtension: "mp3")! 
     do { 
      audioPlayer = try AVAudioPlayer(contentsOf: url) 
      guard let audioPlayer = audioPlayer else { return } 
      audioPlayer.prepareToPlay() 
      audioPlayer.play() 
     } catch let error { 
      print(error.localizedDescription) 
     } 
    } 
} 

现在,在你的任何VC的,你声明这个新Globals类的类范围的变量,并通过预先globalVars

class SettingsViewController: UITableViewController { 
    // MARK: - IBOutlets 
    @IBOutlet weak var backgroundgMusicLabel: UILabel! 
    @IBOutlet weak var backgroundMusicSwitch: UISwitch! 

    let globalVars = Globals.sharedInstance() 

    // MARK: - Lifecycle 
    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    @IBAction func backgroundMusicPressed(_ sender: Any) { 
     if backgroundMusicSwitch.isOn { 
      UserDefaults().set("On", forKey: "switch") 
      self.globalVars.audioPlayer.play() //Uses the same instance of the audioPlayer you created in the 'Globals' class 
     } else { 
      UserDefaults().set("Off", forKey: "switch") 
      self.globalVars.audioPlayer.stop() 
     } 
    } 
} 
使用任何从它的对象

由于您的playMusic函数已移至此新类,因此您将以同样的方式调用它。

self.globalVars.playMusic() 

我知道有很多人在使用所谓的“全局变量”皱眉,不过,我觉得这个共享的实例类在很多情况下是非常有用的。尤其是游戏。这也是我如何保持所有扩展,子类,可以重新使用的自定义函数等的“正在运行”的类。我只将这个文件包含在我的所有项目中,并且可以立即访问所有我最喜欢的函数。