function calls print but won't call anything else inside

Hey guys I am building an app so when the enemy collides with your “boat” then you lose and it will change scene.I have it so when the boat detects collision it will call the function but it only calls the “print” and it wont call the “composer.gotoScene(”")" or anything else inside.

local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view local physics = require ("physics"); physics.start(); physics.setGravity(0,0) local ground = display.newImage("bg.png",240,150,600,600) ground:scale(0.57,0.68) ground:rotate(-90) local cannon1 = display.newImage("cannon.png",math.random(20,90),50) cannon1:scale(0.15,0.15) local cannon2 = display.newImage("cannon.png",math.random(10,300),270,30,30) cannon2:scale(0.15,0.15) local money = 0 local bullets1 = 10 local bullets2 = 10 local numberSmiles = 1 --local variable; amount can be changed local moneydis = display.newText(money,100,70,native.systemFont,24) local boat = display.newRect(0,99,30,400) physics.addBody(boat,"dynamic") local function die(event) print("you lose") composer.removeScene("level1") composer.gotoScene("mainmenu") end boat:addEventListener("collision",die) local function spawnSmiles() for i=1,numberSmiles do local zombie1 = display.newImage("zombie.png",430,50, 45, 45); -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default physics.addBody(zombie1,"dynamic") transition.to( zombie1, { time=math.random(2000,8000), x=-10, y=50} ); -- Note: any time less than one frame period is the same as waiting one frame if cannon1.x\<zombie1.x then if bullets1 \>= 1 then print("shoot") local bullet1 = display.newRect(cannon1.x,cannon1.y,30,10) transition.to(bullet1, { time=9000, x=zombie1.x, y=zombie1.y} ); physics.addBody(bullet1,"static") bullets1 = bullets1-1 moneydis.text = money if bullets1\<=2 then local reloadtxt= display.newText("Reload Cannon 1",300,50,native.systemFont,25) reloadtxt:setFillColor(0,0,0) local function reload1( self,event ) if money\>10 then bullets1= bullets1+10 reloadtxt:removeSelf() end end reloadtxt:addEventListener("touch",reload1) end local function bulletcollision(self, event ) display.remove(bullet1) display.remove(zombie1) money=money+math.random(0,2) end bullet1:addEventListener("collision",bulletcollision) end end end end timer.performWithDelay( 700, spawnSmiles, 0 ) --fire every 10 seconds local function spawnSmiles1() for i=1,numberSmiles do local zombie2 = display.newImage("zombie.png",430,270, 45, 45); physics.addBody(zombie2,"dynamic") -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default transition.to( zombie2, { time=math.random(6000,8000), x=-10, y=270} ); if cannon2.x\<zombie2.x then if bullets2\>=1 then print("shoot") if bullets2\<=0 then local reloadtxt= display.newText("Reload Cannon 2",300,150,native.systemFont,25) end local bullet2 = display.newRect(cannon2.x,cannon2.y,30,10) transition.to(bullet2, { time=9000, x=zombie2.x, y=zombie2.y} ); physics.addBody(bullet2,"static") local function collision2( event ) bullet2:removeSelf() zombie2:removeSelf() money=money+2 bullets2=bullets2-1 end bullet2:addEventListener("collision",collision2) end end end end timer.performWithDelay( 700, spawnSmiles1, 0 ) --fire every 10 seconds local function spawnSmiles3() for i=1,numberSmiles do local zombie3 = display.newImage("zombie.png",430,150, 45, 45); physics.addBody(zombie3,"dynamic") transition.to( zombie3, { time=math.random(6000,8000), x=-10, y=150} ); local function shoothuman( event ) if money\>=5 then zombie3:removeSelf() money = money- 5 money=money+3 moneydis.text = money end end zombie3:addEventListener("touch",shoothuman) end end timer.performWithDelay( 1000, spawnSmiles3, 0 ) --fire every 10 seconds 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. 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 -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

You can’t call composer.removeScene from within the scene you are trying to remove.

Hey figured it out! It was a simple error. I did not insert the objects into a display group. :wink: :wink: :angry:

You can’t call composer.removeScene from within the scene you are trying to remove.

Hey figured it out! It was a simple error. I did not insert the objects into a display group. :wink: :wink: :angry: