Add sound button to game

Hi

I need help with adding sound off/on button to my game. In global variable  lua file, I have the following:

local sounds = {} sounds["select"] = audio.loadSound("sounds/select.mp3") sounds["score"] = audio.loadSound("sounds/score.mp3") G.playSound = function(name)  if sounds[name] ~= nil then  audio.play(sounds[name]) end end

In games.lua file, I call the function as:
 

utils.playSound("score")

I have a soundon.png and soundoff.png files both in a sprite sheet ( not sure if that is a good idea ) and I want to integrate it into the game. Any help would be appreciated.

Hi @sysads,

Is your question about getting the sound toggle working, or the button/switch? If you need to implement the button/switch, I suggest that you use a widget switch (checkbox style) for this. It can accept an image (sprite) sheet as a visual option, and you can use your existing frames as the “on” and “off” states of the switch.

https://docs.coronalabs.com/api/library/widget/newSwitch.html

Take care,

Brent

Hi Brent,

All I am trying to do is have a sound button which when pressed disable all button click sound and game sound effects and that will also show the soundoff image. Then vice versa

I would create a global flag to play the sound and set the icon.

PlaySound = true -- or false. Set this in your button listener if PlaySound = true then utils.playSound("score") end

if PlaySound = true then mySoundIcon = soundOnIcon else mySoundIcon = soundOffIcon end

Hi @sysads,

Is your question about getting the sound toggle working, or the button/switch? If you need to implement the button/switch, I suggest that you use a widget switch (checkbox style) for this. It can accept an image (sprite) sheet as a visual option, and you can use your existing frames as the “on” and “off” states of the switch.

https://docs.coronalabs.com/api/library/widget/newSwitch.html

Take care,

Brent

Hi Brent,

All I am trying to do is have a sound button which when pressed disable all button click sound and game sound effects and that will also show the soundoff image. Then vice versa

I would create a global flag to play the sound and set the icon.

PlaySound = true -- or false. Set this in your button listener if PlaySound = true then utils.playSound("score") end

if PlaySound = true then mySoundIcon = soundOnIcon else mySoundIcon = soundOffIcon end