I’m attempting to change the sound effect volume of a falling acorn as collides with ground based on each impact’s postCollision force.
Unfortunately, I can’t seem get the volume to adjust when I pass a channel to audio.play. It always plays at max volume. However, if I instead set the master volume it works like a champ. Would someone mind helping me see the error of my ways?
[lua]local function acornSoundFinished(event)
audio.dispose(event.handle)
acornSounds = acornSounds - 1
end
local function onPostCollision( event )
if(event.force > 10 and acornSounds < 1 ) then
acornSounds = acornSounds + 1
local acorndrop = audio.loadSound(“acorndrop.mp3”)
local c = audio.findFreeChannel()
local result = audio.setVolume(math.min(event.force/100,1),{ channel=c } )
local volume = audio.getVolume( { channel=c } )
print(c, volume, result)
audio.play(acorndrop, {channel=c, onComplete=acornSoundFinished})
audio.setVolume( 1, { channel=c} )
end
end[/lua]
The output of print when I drop an acorn to the ground is:
3 1 true
3 0.37175366282463 true
3 0.24506214261055 true
[import]uid: 60707 topic_id: 14029 reply_id: 314029[/import]