Random multiple defeat sound play back.

I codes my charters death sound so it will randomly play 8 different sayings. I feel it’s not random enough and that it gets stuck on one defeat for the duration of repeated game play and only changes when you exit the game go to a shop menu. I think it loads up a random sound at the begin of the game but then it keeps using the selected sound instead of changing it after every defeat. Here is my code to load the sound


–Sounds

local starSound = audio.loadSound(“sounds/Collect.mp3”)   --Load sounds

local jumpSound = audio.loadSound(“sounds/Jump.mp3”)

defeat = {}

defeat[1] = audio.loadSound(“sounds/defeat1.mp3”)

defeat[2] = audio.loadSound(“sounds/defeat2.mp3”)

defeat[3] = audio.loadSound(“sounds/defeat3.mp3”)

defeat[4] = audio.loadSound(“sounds/defeat4.mp3”)

defeat[5] = audio.loadSound(“sounds/defeat5.mp3”)

defeat[6] = audio.loadSound(“sounds/defeat6.mp3”)

defeat[7] = audio.loadSound(“sounds/defeat7.mp3”)

defeat[8] = audio.loadSound(“sounds/defeat8.mp3”)

local thisDefeat = math.random(#defeat)

    local starChannel, jumpChannel, overChannel  --Channel vars, used to play sounds


here is how i call the defeat sound


–Picking up bones and powerups…

elseif name1 == “star” or name2 == “star” then

if name1 == “star” then display.remove(event.object1); event.object1 = nil; 

else display.remove(event.object2); event.object2 = nil; end

changeText(1)

if _G.gameStatus.music == true then

starChannel = audio.play(starSound)

end

–Player hits the obstacles

elseif name1 == “vine” or name2 == “vine” then

–Kill player…

gameIsActive = false

gameOverCalled = true

gameOver()

if _G.gameStatus.music == true then

   overChannel = audio.play(defeat[thisDefeat])

end

end

end


thank you

Looks like you a re picking the ‘thisDefeat’ value when you load the sounds in, move that line to just before you play it.

Yes. moving the “thisDefeat” Work.

–Local thisDefeat = math.random(#defeat)

to just before 

if _G.gameStatus.music == true then

   overChannel = audio.play(defeat[thisDefeat])

 

 

Thank you for your help and Time

Looks like you a re picking the ‘thisDefeat’ value when you load the sounds in, move that line to just before you play it.

Yes. moving the “thisDefeat” Work.

–Local thisDefeat = math.random(#defeat)

to just before 

if _G.gameStatus.music == true then

   overChannel = audio.play(defeat[thisDefeat])

 

 

Thank you for your help and Time