Non-responsive touch function but no error .

In my restart screen I used to have an error that is fixed now .But now the play button that I have in my restart screen,when touched does nothing .I don’t get  this because in my menu.lua file I have the same play button that works .

Restart.lua:

local composer = require( "composer" ) local scene = composer.newScene() function scene:create( event ) end function scene:show( event ) end -- display background image local background = display.newImage( "background.png" ) background.x = 10; background.y = 308 -- display ground image local ground = display.newImage( "ground.png" ) ground.x = 145; ground.y = 480 local loseTxt = display.newImageRect( "loseTxt.png", 335, 200 ) loseTxt.x = 160 loseTxt.y = 30 -- display play button image local button = display.newImageRect( "button.png", 110, 80 ) button.x = 160 button.y = 300 function button:touch(event) if event.phase == "ended" then composer.gotoScene("level1", "fade", 200) end return true end button:addEventListener("touch", button) function scene:hide( event ) end if ( phase == "will" ) then elseif ( phase == "will" ) then end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene 

Thank you.

This should work for you:

local composer = require( "composer" ) local scene = composer.newScene() local background, ground, loseTxt, button function scene:create( event ) local sceneGroup = self.view background = display.newImage( "crate.png" ) background.x = 10; background.y = 308 ground = display.newImage( "grass.png" ) ground.x = 145; ground.y = 480 loseTxt = display.newImageRect( "crate.png", 335, 200 ) loseTxt.x = 160 loseTxt.y = 30 button = display.newImageRect( "button.png", 110, 80 ) button.x = 160 button.y = 300 function button:touch(event) if event.phase == "ended" then composer.gotoScene("level1", "fade", 200) end return true end button:addEventListener("touch", button) sceneGroup:insert(background) sceneGroup:insert(ground) sceneGroup:insert(loseTxt) sceneGroup:insert(button) end 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. 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. 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. end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

However there were several problems with your code that I imagine you have in your level1.lua scene also.

The reason you got no error was that you had things set up wrong. You had composer.gotoScene(“level1”, “fade”, 500) , as you should, and it did what it should. It did go to the level1 scene, only it didn’t remove your display objects from your restart scene. This is because you never let composer know about these objects properly. Notice that in each of the composer functions above there is the following line:

local sceneGroup = self.view

self.view is the display group of the current scene. For composer to manage your objects they need to be put into this group as I’ve done for you. This allows composer to move them out of the way when you change scene.

Furthermore, you should probably create these objects inside of the scene:create( ) function. I think you can get away with doing it elsewhere, but it’s really the most natural place for it.

@hasty,

Thank you so so much for your help I realy appreciate it !

This should work for you:

local composer = require( "composer" ) local scene = composer.newScene() local background, ground, loseTxt, button function scene:create( event ) local sceneGroup = self.view background = display.newImage( "crate.png" ) background.x = 10; background.y = 308 ground = display.newImage( "grass.png" ) ground.x = 145; ground.y = 480 loseTxt = display.newImageRect( "crate.png", 335, 200 ) loseTxt.x = 160 loseTxt.y = 30 button = display.newImageRect( "button.png", 110, 80 ) button.x = 160 button.y = 300 function button:touch(event) if event.phase == "ended" then composer.gotoScene("level1", "fade", 200) end return true end button:addEventListener("touch", button) sceneGroup:insert(background) sceneGroup:insert(ground) sceneGroup:insert(loseTxt) sceneGroup:insert(button) end 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. 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. 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. end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

However there were several problems with your code that I imagine you have in your level1.lua scene also.

The reason you got no error was that you had things set up wrong. You had composer.gotoScene(“level1”, “fade”, 500) , as you should, and it did what it should. It did go to the level1 scene, only it didn’t remove your display objects from your restart scene. This is because you never let composer know about these objects properly. Notice that in each of the composer functions above there is the following line:

local sceneGroup = self.view

self.view is the display group of the current scene. For composer to manage your objects they need to be put into this group as I’ve done for you. This allows composer to move them out of the way when you change scene.

Furthermore, you should probably create these objects inside of the scene:create( ) function. I think you can get away with doing it elsewhere, but it’s really the most natural place for it.

@hasty,

Thank you so so much for your help I realy appreciate it !