SpriteKit中的音频调度-Swift

SpriteKit中的音频调度-Swift,swift,sprite-kit,scheduled-tasks,skaction,Swift,Sprite Kit,Scheduled Tasks,Skaction,我试着在一个游戏菜单中播放随机的声音,实际上是鸟的吱吱声。 所以有很多鸟的声音,但我希望它们是随机的 我以前曾使用时间表按照以下方式完成此操作: this->schedule(schedule_selector(HelloWorld::birdsound),3.2); 其中: void HelloWorld::birdsound(){ int soundnum=arc4random()%9+1; switch (soundnum) { case 1:

我试着在一个游戏菜单中播放随机的声音,实际上是鸟的吱吱声。 所以有很多鸟的声音,但我希望它们是随机的

我以前曾使用
时间表
按照以下方式完成此操作:

 this->schedule(schedule_selector(HelloWorld::birdsound),3.2);
其中:

void HelloWorld::birdsound(){
    int soundnum=arc4random()%9+1;

    switch (soundnum) {
        case 1:
            appdelegate->bird1();
            break;
        case 2:
            appdelegate->bird2();
            break;
          .
          .
          .
        case 9:
            appdelegate->bird9();
            break;
        default:
            break;
    }
}
因此,播放随机声音,例如,
bird1()

}


我如何在Spritekit/swift中实现类似的功能,在这里我可以以随机顺序拥有
X
数量的声音文件(或鸟叫),中间有一个小间隙(或等待)?这可以通过
SKActions
实现吗?

您可以将声音文件放入一个数组,设置一个音频播放器,然后创建一个随机播放声音的函数。然后使用一个计时器,它可以在您想要的任何时间间隔调用函数。因此:

Class GameScene {
    var soundFiles = ["bird_sound1", "bird_sound2"]
    var audioPlayer: AVAudioPlayer = AVAudioPlayer()

func setupAudioPlayer(file: NSString, type: NSString){
    let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
    let url = NSURL.fileURLWithPath(path!)
        do {
            try  audioPlayer = AVAudioPlayer(contentsOfURL: url)
        } 
            catch {
            print("Player not available")
        }
    }

func playRandomSound() { 
    let range: UInt32 = UInt32(soundFiles.count)
    let number = Int(arc4random_uniform(range))

    self.setupAudioPlayer(soundFiles[number], type: "wav")

    self.audioPlayer.play()
}

override func didMoveToView(view: SKView) {
    _ = NSTimer.scheduledTimerWithTimeInterval(7, target: self, selector: #selector(GameScene.playRandomSound), userInfo: nil, repeats: true)

}

你可以做的是将声音文件放入一个数组,设置一个音频播放器,然后创建一个随机播放声音的函数。然后使用一个计时器,它可以在您想要的任何时间间隔调用函数。因此:

Class GameScene {
    var soundFiles = ["bird_sound1", "bird_sound2"]
    var audioPlayer: AVAudioPlayer = AVAudioPlayer()

func setupAudioPlayer(file: NSString, type: NSString){
    let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
    let url = NSURL.fileURLWithPath(path!)
        do {
            try  audioPlayer = AVAudioPlayer(contentsOfURL: url)
        } 
            catch {
            print("Player not available")
        }
    }

func playRandomSound() { 
    let range: UInt32 = UInt32(soundFiles.count)
    let number = Int(arc4random_uniform(range))

    self.setupAudioPlayer(soundFiles[number], type: "wav")

    self.audioPlayer.play()
}

override func didMoveToView(view: SKView) {
    _ = NSTimer.scheduledTimerWithTimeInterval(7, target: self, selector: #selector(GameScene.playRandomSound), userInfo: nil, repeats: true)

}

使用
SKAction
我实现了如下:

加载声音:

let cheep1 = SKAction.playSoundFileNamed("bird1.mp3", waitForCompletion: false)
//and so on
创建等待时间和永久调用序列:

func beginRandomCheeping(){
    let sequence = (SKAction.sequence([
        SKAction.waitForDuration(2.0),
        SKAction.runBlock(self.playRandomSound)
    ]))

    let repeatThis = SKAction.repeatActionForever(sequence)
    runAction(repeatThis)
}
随机播放声音:

func playRandomSound() {
    let number = Int(arc4random_uniform(UInt32(9)))

    switch (number){
    case 1:
        runAction(cheep1)
        break
    case 2:
        runAction(cheep2)
        break
       .
       .
       .
    case 9:
        runAction(cheep9)
        break
    default:
        break
    }
}

只需调用self。beginrandomcheping()游戏逻辑中的某个地方。

使用
SKAction
我这样实现它:

加载声音:

let cheep1 = SKAction.playSoundFileNamed("bird1.mp3", waitForCompletion: false)
//and so on
创建等待时间和永久调用序列:

func beginRandomCheeping(){
    let sequence = (SKAction.sequence([
        SKAction.waitForDuration(2.0),
        SKAction.runBlock(self.playRandomSound)
    ]))

    let repeatThis = SKAction.repeatActionForever(sequence)
    runAction(repeatThis)
}
随机播放声音:

func playRandomSound() {
    let number = Int(arc4random_uniform(UInt32(9)))

    switch (number){
    case 1:
        runAction(cheep1)
        break
    case 2:
        runAction(cheep2)
        break
       .
       .
       .
    case 9:
        runAction(cheep9)
        break
    default:
        break
    }
}
只需调用self。在游戏逻辑中的某个地方开始随机设置()