What's wrong with my button toggle function?

I’m trying to get a button to toggle between on and off states, but it only works once. Repeated presses seemingly have no effect. Here’s my code:
local musicOn_btn = display.newImageRect("musicOn_btn.png", 343, 72);musicOn_btn:setReferencePoint(display.CenterReferencePoint);musicOn_btn.x = _W/2; musicOn_btn.y = 275;local musicOff_btn = display.newImageRect("musicOff_btn.png", 343, 72);musicOff_btn:setReferencePoint(display.CenterReferencePoint);musicOff_btn.x = _W/2; musicOff_btn.y = 275;musicOff_btn.isVisible = falsefunction toggleMusicButton(event) if(event.phase == "ended") then if musicOn_btn.isVisible == true then musicOff_btn.isVisible = true musicOn_btn.isVisible = false; elseif musicOn_btn.isVisible == false then musicOff_btn.isVisible = false musicOn_btn.isVisible = true; end endendmusicOn_btn:addEventListener("touch", toggleMusicButton)[/code]I'm not much of a coder, so any help would be much appreciated.Thanks in advance!Steven [import]uid: 79394 topic_id: 19893 reply_id: 319893[/import]

Your off button has no listener.

add:

musicOff_btn:addEventListener(“touch”, toggleMusicButton)
and probably you can replace

elseif musicOn_btn.isVisible == false then
by just

else
[import]uid: 108660 topic_id: 19893 reply_id: 77272[/import]

musicOn_btn.isHitTestable = true

this should work for you, try it) [import]uid: 16142 topic_id: 19893 reply_id: 77273[/import]

Yeap, sure did!

Thanks darkC.

Btw, do you happen to know the technical explanation as to why my code wasn’t working? I’m glad my problem is fixed, but I’m still curious why it was there in the first place! [import]uid: 79394 topic_id: 19893 reply_id: 77279[/import]

I got around having to add an event listener by using darkconsoles’ suggestion.

But then I tried your method, and it worked as well. I’m guessing my problem was that my musicOn_btn stopped listening once it became invisible.

Also, the shortening of the elseif statement was really helpful in cleaning up my code.

Thanks for your advice, Jeff472!

Steven [import]uid: 79394 topic_id: 19893 reply_id: 77281[/import]