启动游戏时停止背景音乐

时间:2023-01-22 22:35:08

I have background music which starts when the app is launched in GameViewController.swift using the following code:

我有背景音乐当应用在GameViewController中启动时开始。swift使用以下代码:

class GameViewController: UIViewController {

// VARIABLES
var backgroundMusicPlayer : AVAudioPlayer!

// AUDIO PLAYER
func playBackgroundMusic(filename: String) {
    let url = NSBundle.mainBundle().URLForResource(filename, withExtension: nil)
    var error : NSError? = nil
    do {
        backgroundMusicPlayer = try AVAudioPlayer(contentsOfURL: url!)
    } catch let error1 as NSError {
        error = error1
        backgroundMusicPlayer = nil
    }
    if backgroundMusicPlayer == nil {
        print("Could not create audio player: \(error!)")
        return
    }
    backgroundMusicPlayer.numberOfLoops = -1
    backgroundMusicPlayer.prepareToPlay()
    backgroundMusicPlayer.play()
}

func stopBackgroundMusic() {
    backgroundMusicPlayer.stop()
}

override func viewDidLoad() {
    super.viewDidLoad()
    playBackgroundMusic("MainTheme.mp3")

<< Various irrelevant code >>
}

Because this is run in the viewController, it persists through changing scenes on the menu (i.e. opening the "shop" scene) and creates a seamless track. When I click the "Play" button on the menu scene I want the music to then stop, and transition to the game. I have the stopBackgroundMusic() method in the GameViewController but I don't know how to call it on on the menu scene. IN THE MENU SCENE I tried this:

因为这是在viewController中运行的,它通过菜单上的变化场景(即打开“商店”场景)而持续,并创建一个无缝的跟踪。当我点击菜单上的“播放”按钮时,我希望音乐停止,并向游戏过渡。我在GameViewController中有stopBackgroundMusic()方法,但我不知道如何在菜单场景中调用它。在菜单场景中,我尝试了以下方法:

// TOUCH
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first as UITouch?
    let touchLocation = touch!.locationInNode(self)
    let touchedNode = self.nodeAtPoint(touchLocation)
    if touchedNode.name == "startGame" {
        GameViewController.stopBackgroundMusic()
        let transitionType = SKTransition.fadeWithDuration(2)
        let viewSize = self.view?.bounds.size
        let scene = GameScene(size: viewSize!)
        self.view?.presentScene(scene, transition: transitionType)
    }
}

But I get an error saying I'm missing parameter #1 in call for stopBackgroundMusic() which shouldn't require any parameters. Am I calling this method wrong? Thanks!

但是我有一个错误,说我缺少参数#1,在调用stopBackgroundMusic()时,它不需要任何参数。我把这个方法称为错误的吗?谢谢!

1 个解决方案

#1


2  

You are referring to your class by using GameViewController but your function is at the object instance level.

你使用GameViewController来引用你的类,但是你的函数在对象实例级。

If you declare the variable and function at the class level, your code in the touchesBegan function should work fine.

如果在类级别声明变量和函数,则touchesBegan函数中的代码应该可以正常工作。

static var backgroundMusicPlayer : AVAudioPlayer!
class func playBackgroundMusic(filename: String) ...
class func stopBackgroundMusic()

override func viewDidLoad() {
   super.viewDidLoad()
   GameViewController.playBackgroundMusic("MainTheme.mp3")

  << Various irrelevant code >>
}

#1


2  

You are referring to your class by using GameViewController but your function is at the object instance level.

你使用GameViewController来引用你的类,但是你的函数在对象实例级。

If you declare the variable and function at the class level, your code in the touchesBegan function should work fine.

如果在类级别声明变量和函数,则touchesBegan函数中的代码应该可以正常工作。

static var backgroundMusicPlayer : AVAudioPlayer!
class func playBackgroundMusic(filename: String) ...
class func stopBackgroundMusic()

override func viewDidLoad() {
   super.viewDidLoad()
   GameViewController.playBackgroundMusic("MainTheme.mp3")

  << Various irrelevant code >>
}