Reputation: 29
I have a single sound file playing at the start of my game:
// Play the start sound:
self.run(SKAction.playSoundFileNamed("Sound/StartGame.aif", waitForCompletion: false))
How do I get a random single sound file from an array to play instead?
Upvotes: 1
Views: 875
Reputation: 4383
Try this:
let soundNames = ["soundName1", "soundName2", "soundName3"]
let randomSoundName = soundNames[Int(arc4random_uniform(UInt32(soundNames.count)))]
let randomSound = self.run(SKAction.playSoundFileNamed(randomSoundName, waitForCompletion: false))
Upvotes: 1
Reputation: 3657
First of all, you have to add all sound name in the array and get the random name of sound from the array.
let arrSound = ["Sound/StartGame.aif","Sound/StartGame2.aif"]
Get the random name of sound.
let randomSound = arrSound[Int(arc4random_uniform(arrSound.count))]
self.run(SKAction.playSoundFileNamed(randomSound, waitForCompletion: false))
Upvotes: 0
Reputation: 11577
Create a array containing all the music file names eg.Sound/StartGame.aif
and pick one randomly when you need to play sound.
Upvotes: 0