Stacked Buttons - Swap

I’m trying to create a sound button that when pressed, the Sound On Button  is swapped for the Sound Off Button.  I’ve tried several things but I always have the same issue.  I press the Sound On button and the Sound Off Button is displayed but I’m not able to press the Sound Off Button to make it switch to the Sound On Button.  

Although the Sound Off is displayed, the terminal response indicates that the code is still recognizing the Sound On button and continues to update and save the Sound On.  I would appreciate any suggestions.  

The button is on an overlay screen.  Below is my code

  • -----------------------------------------------------------------------------------------------

FORWARD DECLARATIONS - IN COMPOSER

------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------- 

–SOUND ON BUTTON PRESSED EVENT

-------------------------------------------------------------------------------------------- 

function soundOnButtonPressed (event)

local soundOnBtn = event.target

local phase = event.phase

if “began” == phase then

display.getCurrentStage():setFocus( soundOnBtn )

soundOnBtn.isFocus = true

soundOn =0    

soundOff = 1  

soundOnBtn.isVisible = false

soundOnBtn.isActive =  false

soundOffBtn.isVisible = true

soundOffBtn.isActive = true

elseif “ended” == phase or “cancelled” == phase then

display.getCurrentStage():setFocus( nil )

soundOnBtn.isFocus = false

end

saveSoundStatusUpdate()

end

-------------------------------------------------------------------------------------------- 

–SOUND OFF BUTTON PRESSED EVENT

-------------------------------------------------------------------------------------------- 

function soundOffButtonPressed (event)

local soundOffBtn = event.target

local phase = event.phase

if “began” == phase then

soundOff =0    

soundOn = 1  

soundOffBtn.isVisible = false

soundOffBtn.isActive =  false

soundOffBtn.isVisible = true

soundOffBtn.isActive = true

elseif “ended” == phase or “cancelled” == phase then

display.getCurrentStage():setFocus( nil )

soundOffBtn.isFocus = false

end

saveSoundStatusUpdate()

end

  • -----------------------------------------------------------------------------------------------

SCENE CREATE - IN COMPOSER

------------------------------------------------------------------------------------------------


–SOUND ON IMAGE


soundOnBtn =display.newImage(“Images/soundOnBtn.png”)

soundOnBtn.isVisible = true

soundOnBtn.isActive = true 

soundOnBtn.id = “soundOnBtn”

soundOnBtn.x= 300 ; soundOnBtn.y= 684 

onRelease = soundOnButtonPressed 

sceneGroup:insert(soundOnBtn) 

overlayBtnGroup:insert(soundOnBtn)


–SOUND OFF IMAGE


 soundOffBtn =display.newImage(“Images/soundOffBtn.png”)  

soundOffBtn.isVisible = false

soundOffBtn.isActive = false

soundOffBtn.id = “soundOffBtn”

soundOffBtn.x= 300 ; soundOffBtn.y= 684 

onRelease = soundOffButtonPressed

sceneGroup:insert(soundOffBtn)

overlayBtnGroup:insert(soundOffBtn)

  • -----------------------------------------------------------------------------------------------

SCENE SHOW - IN COMPOSER

------------------------------------------------------------------------------------------------

elseif phase == “did” then

function loadsoundOn()

soundOn = mySettings.loadsoundOn(“soundOnfile.txt”)

soundOn = tonumber(soundOn)

    if soundOn == nil then

       soundOn = 0

       soundOn = tonumber(soundOn)

   elseif soundOn > 0 then

        

 end

loadsoundOff()

end


function loadsoundOff()

     soundOff = mySettings.loadsoundOn(“soundOfffile.txt”)

     soundOff = tonumber(soundOff)

     if soundOff == nil then

        soundOff = 0

        soundOff = tonumber(soundOff)

     elseif soundOff > 0 then

     end

     soundStatus()

end


function soundStatus()

     if soundOn == 1 then

        soundOnBtn.isVisible = true

        soundOnBtn.isActive = true

        soundOffBtn.isVisible = false

       soundOffBtn.isActive = false

     elseif soundOff == 1 then

        soundOnBtn.isVisible = false

        soundOnBtn.isActive = false

        soundOffBtn.isVisible = true

        soundOffBtb.isActive = true

    end

 end


function saveSoundStatusUpdate()

 ----------------------------------------------

–SET SOUND


print (“Running function saveSoundStatusUpdate”)

mySettings.setsoundOn(soundOn)

mySettings.setsoundOff(soundOff)


–SAVE SOUND


mySettings.savesoundOn(“soundOnfile.txt”)

mySettings.savesoundOff(“soundOfffile.txt”)

end

soundOnBtn:addEventListener(“touch”, soundOnButtonPressed)

soundOffBtn:addEventListener(“touch”, soundOffButtonPressed)

I figured out my problem.  In the sound off event I copied didn’t turn on the sound object and so it caused the sound off to remain active.

All is well.  Have a great weekend.

I figured out my problem.  In the sound off event I copied didn’t turn on the sound object and so it caused the sound off to remain active.

All is well.  Have a great weekend.