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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 |