Music button doesn't work

My music button doesn’t appear on my menu. The thing that im doing is a music button that disable itself after you click it. The other buttons are visible in my menu but the music button isnt… line 40 and 43 

function scene:create( event ) local sceneGroup = self.view local background = display.newImage("images/background2.jpeg", display.contentHeight, display.contentWidth) background:addEventListener( "touch", stopTouches ) background:addEventListener( "tap", stopTouches ) background.anchorX = 0 background.anchorY = 0 background.x, background.y = 0, 0 local playBtn = widget.newButton{ defaultFile="images/Buttons/playBtn.png", overFile="images/Buttons/playBtn1.png", width=350, height=250, onRelease = onplayBtnRelease -- event listener function } playBtn.x = 500 playBtn.y = 875 local leaderBtn = widget.newButton{ defaultFile="images/Buttons/leaderboardsBtn.png", overFile="images/Buttons/leaderboardsBtn1.png", width=700, height=250, onRelease = onLeaderBoardsBtnRelease -- event listener function } leaderBtn.x = 500 leaderBtn.y = 1175 local multiplayerBtn = widget.newButton{ defaultFile ="images/Buttons/multiBtn.png", overFile ="images/Buttons/multiBtn1.png", width=700, height=250, onRelease = onMultiplayerBtnRelease -- event listener function } multiplayerBtn.x = 500 multiplayerBtn.y = 1475 end local mainMenu = display.newGroup() local musicOff = display.newImageRect(mainMenu,"images/Buttons/Music1.png", 300, 300 ) musicOff.x = 690 musicOff.y = 1750 local musicOn = display.newImageRect(mainMenu, "images/Buttons/Music.png", 300, 300 ) musicOn.x = 690 musicOn.y = 1750 if (musicEnabled) then musicOff.alpha = 0 else musicEnabled = off musicOn.alpha = 1 end local function onMusicButtonTouch( event ) if "began" == event.phase then print("music button is pressed.") if (musicEnabled) then musicOff.alpha = 1 musicOn.alpha = 0 audio.pause("sound/gameMusic.mp3") musicEnabled = false print("Music disabled") else musicOn = 1 musicOff.alpha = 0 audio.play("sound/gameMusic.mp3") musicEnabled = true print("Music enable") end end return true end musicOff:addEventListener("touch", onMusicButtonTouch) musicOn:addEventListener("touch", onMusicButtonTouch) mainMenu.musicOff = musicOff mainMenu.musicOff = musicOn

move all the lines 39-52 and put them inside the ‘scene:create’ function.  So put those lines 39-52 after line 36, and before the ‘end’ statement at line 37.

also be sure to insert all ‘display objects’ including mainMenu into sceneGroup.  That is very important to do.

You may have done that already, and as I quickly looked over the code maybe I missed it, but if you have not inserted all the display objects into sceneGroup, you really need to do that.

Good luck.

Bob

In line 64 you replace the display object musicOn with a number.

Rob

I already inserted it bob by the way im having an error (menu.lua: 75) attempt to index global ‘musicOff’ a nil value

I already have local variable for musicOff how does it happen? 

at the top of your module is where devs usually would declare the various variables(objects) that need to be accessed throughout the module.

so on line 40 in your post here (that line you moved up into the scene:create function as I advised in my first response), needs to have the ‘local’ removed from it.  Instead add this line  somewhere    local musicOff   near the top of the module 

now it is declared at module scope.  If you were to leave it ‘declared’ as local in the function scene:create it is only visible within that function.

any of the other objects/variables you have inside that function scene:create that you presently have declared as local, you may want to think about if they need to be visible outside that function, and if so, they also need the ‘local’ removed and they then need to be also declared somewhere near the top of the module, as local to the module.

Good luck

Bob

move all the lines 39-52 and put them inside the ‘scene:create’ function.  So put those lines 39-52 after line 36, and before the ‘end’ statement at line 37.

also be sure to insert all ‘display objects’ including mainMenu into sceneGroup.  That is very important to do.

You may have done that already, and as I quickly looked over the code maybe I missed it, but if you have not inserted all the display objects into sceneGroup, you really need to do that.

Good luck.

Bob

In line 64 you replace the display object musicOn with a number.

Rob

I already inserted it bob by the way im having an error (menu.lua: 75) attempt to index global ‘musicOff’ a nil value

I already have local variable for musicOff how does it happen? 

at the top of your module is where devs usually would declare the various variables(objects) that need to be accessed throughout the module.

so on line 40 in your post here (that line you moved up into the scene:create function as I advised in my first response), needs to have the ‘local’ removed from it.  Instead add this line  somewhere    local musicOff   near the top of the module 

now it is declared at module scope.  If you were to leave it ‘declared’ as local in the function scene:create it is only visible within that function.

any of the other objects/variables you have inside that function scene:create that you presently have declared as local, you may want to think about if they need to be visible outside that function, and if so, they also need the ‘local’ removed and they then need to be also declared somewhere near the top of the module, as local to the module.

Good luck

Bob