【问题标题】:Scheduling Audio in SpriteKit - Swift在 SpriteKit 中调度音频 - Swift
【发布时间】:2016-04-29 16:45:20
【问题描述】:

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

我之前使用schedule 完成了此操作,如下所示:

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

void AppDelegate::bird1(){
    CocosDenshion::SimpleAudioEngine::sharedEngine()->stopAllEffects();
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("bird1.mp3");

}

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

【问题讨论】:

    标签: swift sprite-kit scheduled-tasks skaction


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      使用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. beginRandomCheeping()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多