Buttons not working and printing to console won't work

I’m creating a simple slide show for my Directions Page for a Game I’m creating. I have 21 images on the screen that I remove one at a time until the last page which when tapped should send you to the game. All of my buttons work until the very last button. I put print statements in all my functions as the slide show goes along and for some reason only one of my print statements makes it to the console. I can’t figure it out.

This is a small slice of the Code.

local function playTime() audio.stop() composer.gotoScene ( "valley", { effect="fade", time=200 } ) end local function goPage21() print("made 21") print("valley go!") timer.performWithDelay( 1000, playTime ) end dir[21] = display.newImage("Directionspage21.png") dir[21].width= screenWidth dir[21].height = screenHeight dir[21].isVisible=true dir[21].isHitTestable=true dir[21].x = centerX dir[21].y = centerY sceneGroup:insert(dir[21]) dir[21]:addEventListener("tap",goPage21)

Obviously there’s more code but it’s 20 other images set up the same way with similar functions. The only Print Statement that works is the one in goPage4() function none of the other Prints work, even though I know it has gone to that function.

Any explanations?

That’s not very efficient… 

Maybe try this:

local function playTime() audio.stop() composer.gotoScene ( "valley", { effect="fade", time=200 } ) end local dir = {} local function onTouch( self, event ) if( event.phase ~= "ended" ) then return false end print( "Made ", self.myNum ) print("valley go!") timer.performWithDelay( 1000, playTime ) return true end local function newButton( group, x, y, num ) local obj = display.newImage( group, string.format("Directionspage%2.2d.png", num ) ) dir[num] = obj obj.myNum = num obj.width= screenWidth obj.height = screenHeight obj.isVisible=true obj.isHitTestable=true obj.x = x obj.y = y obj.touch = onTouch obj:addEventListener("touch") return obj end -- Just random example to show how much more compact this is for i = 1, 21 do newButton( sceneGroup, centerX, centerY + i \* 20 , i ) end

Probably click propagation.  Try adding “return true” at the end of your event listener functions.

I have return true on everything else so Thanks I will try that Immediately.

That didn’t work.

Did you add it to all 21 functions?

Yes, i had it in all 20 left the last one out by accident but when I put it in it didn’t work. Needless to say it doesn’t explain why the print is only working once in the console. Not sure what is going on.

Still trying to figure this out. Any help would be appreciated. Trying to get my Beta release ready for this Friday. Thanks a bunch.

You’ll need to show us the complete code for us to help

OK. Here it is. It’s just a setup and not Efficient at this point programming wise but I was just trying to set it up and was going to rework everything once it was running which is probably not a good way to go about it but it works for me.

local composer = require( "composer" ) local scene = composer.newScene() local sndMusic = audio.loadStream("directionsounds.mp3") -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- local forward references should go here local centerX = display.contentCenterX local centerY = display.contentCenterY local screenLeft = display.screenOriginX --screenWidth = display.contentWidth - screenLeft \* 2 local screenWidth = display.contentWidth - screenLeft local screenRight = screenLeft + screenWidth local screenTop = display.screenOriginY --screenHeight = display.contentHeight - screenTop \* 2 local screenHeight = display.contentHeight - screenTop local screenBottom = screenTop + screenHeight local centerX = display.contentCenterX local centerY = display.contentCenterY local \_W = display.contentWidth local \_H = display.contentHeight display.setStatusBar( display.HiddenStatusBar ) -- ------------------------------------------------------------------------------- local dir={} local function playTime() audio.stop() composer.gotoScene ( "valley", { effect="fade", time=200 } ) end local function infoStart() print("made start") transition.to(dir[1],{time=1000, alpha=0 }) transition.to(dir[2],{delay=3000,time=1000, alpha=0 }) dir[3].isHitTestable=true return true end print("curse2") local function goPage3() print("made 3") dir[1]:removeSelf() dir[2]:removeSelf() transition.to(dir[3],{time=1000, alpha=0 }) dir[3].isHitTestable=false dir[4].isHitTestable=true return true end local function goPage4() dir[2]:removeSelf() print("made 4") transition.to(dir[4],{time=1000, alpha=0 }) dir[4].isHitTestable=false dir[5].isHitTestable=true dir[3]:removeEventListener( "tap", goPage3 ) end local function goPage5() print("made 5") transition.to(dir[5],{time=1000, alpha=0 }) dir[5].isHitTestable=false dir[6].isHitTestable=true dir[4]:removeEventListener( "tap", goPage3 ) return true end local function goPage6() print("made 6") transition.to(dir[6],{time=1000, alpha=0 }) dir[6].isHitTestable=false dir[7].isHitTestable=true return true end local function goPage7() print("made 7") transition.to(dir[7],{time=1000, alpha=0 }) transition.to(dir[8],{delay=3000,time=1000, alpha=0 }) transition.to(dir[9],{delay=6000,time=1000, alpha=0 }) dir[7].isHitTestable=false dir[10].isHitTestable=true return true end local function goPage10() print("made 10") transition.to(dir[10],{time=1000, alpha=0 }) dir[10].isHitTestable=false dir[11].isHitTestable=true return true end local function goPage11() print("made 11") transition.to(dir[11],{time=1000, alpha=0 }) transition.to(dir[12],{delay=3000,time=1000, alpha=0 }) dir[11].isHitTestable=false dir[13].isHitTestable=true return true end local function goPage13() print("made 13") transition.to(dir[13],{time=1000, alpha=0 }) transition.to(dir[14],{delay=2000,time=1000, alpha=0 }) transition.to(dir[15],{delay=5000,time=1000, alpha=0 }) dir[13].isHitTestable=false dir[16].isHitTestable=true return true end local function goPage16() print("made 16") transition.to(dir[16],{time=1000, alpha=0 }) dir[16].isHitTestable=false dir[17].isHitTestable=true return true end local function goPage17() print("made 17") transition.to(dir[17],{time=1000, alpha=0 }) dir[17].isHitTestable=false dir[18].isHitTestable=true return true end local function goPage18() print("made 18") transition.to(dir[18],{time=1000, alpha=0 }) dir[18].isHitTestable=false dir[19].isHitTestable=true return true end local function goPage19() print("made 19") transition.to(dir[19],{time=1000, alpha=0 }) transition.to(dir[20],{delay=2000,time=1000, alpha=0 }) dir[19].isHitTestable=false dir[21].isHitTestable=true return true end local function goPage21() print("made 21") print("valley go!") timer.performWithDelay( 1000, playTime ) return true end -- "scene:create()" function scene:create( event ) local sceneGroup = self.view print("the curse") -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. -- Background Image dir[21] = display.newImage("Directionspage21.png") dir[21].width= screenWidth dir[21].height = screenHeight dir[21].isVisible=true dir[21].isHitTestable=true dir[21].x = centerX dir[21].y = centerY sceneGroup:insert(dir[21]) dir[21]:addEventListener("touch",goPage21) dir[20] = display.newImage("Directionspage20.png") dir[20].width= screenWidth dir[20].height = screenHeight dir[20].isVisible=true dir[20].isHitTestable=false dir[20].x = centerX dir[20].y = centerY sceneGroup:insert(dir[20]) dir[19] = display.newImage("Directionspage19.png") dir[19].width= screenWidth dir[19].height = screenHeight dir[19].isVisible=true dir[19].isHitTestable=false dir[19].x = centerX dir[19].y = centerY sceneGroup:insert(dir[19]) dir[19]:addEventListener("tap",goPage19) dir[18] = display.newImage("Directionspage18.png") dir[18].width= screenWidth dir[18].height = screenHeight dir[18].isVisible=true dir[18].isHitTestable=false dir[18].x = centerX dir[18].y = centerY sceneGroup:insert(dir[18]) dir[18]:addEventListener("tap",goPage18) dir[17] = display.newImage("Directionspage17.png") dir[17].width= screenWidth dir[17].height = screenHeight dir[17].isVisible=true dir[17].isHitTestable=false dir[17].x = centerX dir[17].y = centerY sceneGroup:insert(dir[17]) dir[17]:addEventListener("tap",goPage17) dir[16] = display.newImage("Directionspage16.png") dir[16].width= screenWidth dir[16].height = screenHeight dir[16].isVisible=true dir[16].isHitTestable=false dir[16].x = centerX dir[16].y = centerY sceneGroup:insert(dir[16]) dir[16]:addEventListener("tap",goPage16) dir[15] = display.newImage("Directionspage15.png") dir[15].width= screenWidth dir[15].height = screenHeight dir[15].isVisible=true dir[15].isHitTestable=false dir[15].x = centerX dir[15].y = centerY sceneGroup:insert(dir[15]) dir[14] = display.newImage("Directionspage14.png") dir[14].width= screenWidth dir[14].height = screenHeight dir[14].isVisible=true dir[14].isHitTestable=false dir[14].x = centerX dir[14].y = centerY sceneGroup:insert(dir[14]) dir[13] = display.newImage("Directionspage13.png") dir[13].width= screenWidth dir[13].height = screenHeight dir[13].isVisible=true dir[13].isHitTestable=false dir[13].x = centerX dir[13].y = centerY sceneGroup:insert(dir[13]) dir[13]:addEventListener("tap",goPage13) dir[12] = display.newImage("Directionspage12.png") dir[12].width= screenWidth dir[12].height = screenHeight dir[12].isVisible=true dir[12].isHitTestable=false dir[12].x = centerX dir[12].y = centerY sceneGroup:insert(dir[12]) dir[11] = display.newImage("Directionspage11.png") dir[11].width= screenWidth dir[11].height = screenHeight dir[11].isVisible=true dir[11].isHitTestable=false dir[11].x = centerX dir[11].y = centerY sceneGroup:insert(dir[11]) dir[11]:addEventListener("tap",goPage11) dir[10] = display.newImage("Directionspage10.png") dir[10].width= screenWidth dir[10].height = screenHeight dir[10].isVisible=true dir[10].isHitTestable=false dir[10].x = centerX dir[10].y = centerY sceneGroup:insert(dir[10]) dir[10]:addEventListener("tap",goPage10) dir[9] = display.newImage("Directionspage9.png") dir[9].width= screenWidth dir[9].height = screenHeight dir[9].isVisible=true dir[9].isHitTestable=false dir[9].x = centerX dir[9].y = centerY sceneGroup:insert(dir[9]) dir[8] = display.newImage("Directionspage8.png") dir[8].width= screenWidth dir[8].height = screenHeight dir[8].isVisible=true dir[8].isHitTestable=false dir[8].x = centerX dir[8].y = centerY sceneGroup:insert(dir[8]) dir[7] = display.newImage("Directionspage7.png") dir[7].width= screenWidth dir[7].height = screenHeight dir[7].isVisible=true dir[7].isHitTestable=false dir[7].x = centerX dir[7].y = centerY sceneGroup:insert(dir[7]) dir[7]:addEventListener("tap",goPage7) dir[6] = display.newImage("Directionspage6.png") dir[6].width= screenWidth dir[6].height = screenHeight dir[6].isVisible=true dir[6].isHitTestable=false dir[6].x = centerX dir[6].y = centerY sceneGroup:insert(dir[6]) dir[6]:addEventListener("tap",goPage6) dir[5] = display.newImage("Directionspage5.png") dir[5].width= screenWidth dir[5].height = screenHeight dir[5].isVisible=true dir[5].isHitTestable=false dir[5].x = centerX dir[5].y = centerY sceneGroup:insert(dir[5]) dir[5]:addEventListener("tap",goPage5) dir[4] = display.newImage("Directionspage4.png") dir[4].width= screenWidth dir[4].height = screenHeight dir[4].isVisible=true dir[4].isHitTestable=false dir[4].x = centerX dir[4].y = centerY sceneGroup:insert(dir[4]) dir[4]:addEventListener("tap",goPage4) dir[3] = display.newImage("Directionspage3.png") dir[3].width= screenWidth dir[3].height = screenHeight dir[3].isVisible=true dir[3].isHitTestable=false dir[3].x = centerX dir[3].y = centerY sceneGroup:insert(dir[3]) dir[3]:addEventListener("tap",goPage3) dir[2] = display.newImage("Directionspage2.png") dir[2].width= screenWidth dir[2].height = screenHeight dir[2].isVisible=true dir[2].isHitTestable=false dir[2].x = centerX dir[2].y = centerY sceneGroup:insert(dir[2]) dir[1] = display.newImage("Directionspage1.png") dir[1].width= screenWidth dir[1].height = screenHeight dir[1].isVisible=true dir[1].isHitTestable=false dir[1].x = centerX dir[1].y = centerY sceneGroup:insert(dir[1]) print("start info") infoStart() end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. local backgroundMusic = audio.play(sndMusic, {channel=2, loops=-1}) end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. audio.stop() elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. composer.removeScene("curse") end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

I wasn’t done writing everything when I came upon this problem so the code is unfinished at the moment. For instance I was just starting to remove objects and event listeners when this problem arose. Thanks for your help.

Correct, this is very inefficient coding. I haven’t looked through all of it but one recurring issue that I see is your prolific use of isHitTestable. You can only use this property if the image is NOT visible. If the image is visible using isHitTestable does nothing.

I’ll take those out and just leave the trues in there. Like I said I was just setting things up and some of the lines will be taken out when I finalize it but it still doesn’t explain the buttons not working and the console only printing “the curse” and “made 4”. Thanks JonPM for helping. Hopefully we can figure this out. Or someone, now that I’ve posted the code.

By glancing at the code it looks like there are a number of things wrong, and it’s quite hard to read/understand what you are trying to do.  To be honest, I would advise you to perhaps let us know what you are trying to achieve in this scene.  It would be easier to help you write the code from the ground up then to try to debug what you have so far.  

Thanks Jon, I mentioned in the initial question that it was a slide show for directions to a game. As they read the directions they press the screen to go to the next sequence of pictures and so on until the last picture which sends them to the game Start. The pictures are one on top of another and I simply remove them as the Player eventually gets through all the directions. Thanks again for your time. The code I posted is basically repeating pieces so there’s not a lot of different code to look at. I just don’t understand why all the buttons work except the last one at 21 and why the console isn’t printing everything. Line 180 should say tap, I just change it to see if I could get a touch response but no luck.

I removed everything except the last page of the sequence and for some reason it still shows all the other pictures even though I have commented everything out so I am not sure what is going on. It doesn’t seem like it’s saving my updates to the code. I opened up the Lua file in a text editor and none of the changes I made have been saved. So that’s the problem with it not working. Any help on this would be awesome!

I think it’s Outlaw not saving my files. I went into the text Lua file and pasted all my new code and everything works fine. No need to rewrite the code. I knew it should work.

That’s not very efficient… 

Maybe try this:

local function playTime() audio.stop() composer.gotoScene ( "valley", { effect="fade", time=200 } ) end local dir = {} local function onTouch( self, event ) if( event.phase ~= "ended" ) then return false end print( "Made ", self.myNum ) print("valley go!") timer.performWithDelay( 1000, playTime ) return true end local function newButton( group, x, y, num ) local obj = display.newImage( group, string.format("Directionspage%2.2d.png", num ) ) dir[num] = obj obj.myNum = num obj.width= screenWidth obj.height = screenHeight obj.isVisible=true obj.isHitTestable=true obj.x = x obj.y = y obj.touch = onTouch obj:addEventListener("touch") return obj end -- Just random example to show how much more compact this is for i = 1, 21 do newButton( sceneGroup, centerX, centerY + i \* 20 , i ) end

Probably click propagation.  Try adding “return true” at the end of your event listener functions.

I have return true on everything else so Thanks I will try that Immediately.

That didn’t work.