在 SpriteKit 中安排音频 - Swift
Scheduling Audio in SpriteKit - Swift
我正在尝试在游戏菜单中播放随机声音,实际上是鸟叫声。
所以有很多鸟叫声,但我希望它们是随机的。
我之前使用 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
来完成吗?
你可以做的是将声音文件放入一个数组,设置一个音频播放器,然后创建一个随机播放声音的函数。然后使用一个计时器,在您想要的任何时间间隔调用该函数。所以:
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. beginRandomCheeping()
。
我正在尝试在游戏菜单中播放随机声音,实际上是鸟叫声。 所以有很多鸟叫声,但我希望它们是随机的。
我之前使用 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
来完成吗?
你可以做的是将声音文件放入一个数组,设置一个音频播放器,然后创建一个随机播放声音的函数。然后使用一个计时器,在您想要的任何时间间隔调用该函数。所以:
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. beginRandomCheeping()
。