This answers your question and will also generally help you out with performance.
I see that you are loading the sound every time it is played. This will hinder performance.
A suggestion to solve this and improve performance is to pre-load all of your sounds into a table.
Here is an example using a modified version of your “playSound” function.
Instead of passing the actual “sound” file name, you’d be passing the “key” of the index in the table.
[lua]local GameSounds =
{
[“S1”] = audio.loadSound( “Sound1.ogg” ),
[“S2”] = audio.loadSound( “Sound2.ogg” ),
[“S3”] = audio.loadSound( “Sound3.ogg” ),
[“S4”] = audio.loadSound( “Sound4.ogg” ),
[“S5”] = audio.loadSound( “Sound5.ogg” )
}
function playSound(sndKey, sndVol)
– If there is no volume don’t play the sound
if (sndVol <= 0) then
return
end
– Find a free channel to play the sound
local sndChanFree = audio.findFreeChannel()
– Set the volume in this free channel
audio.setVolume ( sndVol, { channel=sndChanFree } );
– Play the sound in the free channel
audio.play( GameSounds[sndKey], { channel = sndChanFree } )
return;
end[/lua]
For example, to play the sound called “S4” you would call the function as such:
[lua]playSound(“S4”, 1)[/lua]
I hope that helps you in the future!
Regards,
Andreas Ricci
NuPlay Entertainment
Founder & Lead Developer
[import]uid: 7366 topic_id: 12749 reply_id: 46754[/import]