1

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?

Bootfit
  • 29
  • 4

3 Answers3

1

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))
Krishnarjun Banoth
  • 1,410
  • 1
  • 15
  • 30
Vlad Khambir
  • 4,313
  • 1
  • 17
  • 25
0

Create a array containing all the music file names eg.Sound/StartGame.aif and pick one randomly when you need to play sound.

Saranjith
  • 11,242
  • 5
  • 69
  • 122
0

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))
Salman Ghumsani
  • 3,647
  • 2
  • 21
  • 34