Display object won't remove self on touch.

Hey guys I have run into yet another problem. So when I tap the jellys it is supposed to destroy them and add points but right now it only adds points and does not destroy them.There is a function called removeit that is where i remove the jellys.

Thanks Tyler Jacobson​:slight_smile::slight_smile::slight_smile::slight_smile:

--game.lua 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 actualWidth = display.actualContentWidth local actualHeight = display.actualContentHeight local actualWidth1 = display.actualContentWidth/2 local actualHeight1 = display.actualContentHeight/2 local centerX = display.contentCenterX local centerY = display.contentCenterY local jelly = nil local BG = display.newImage("BG.png",centerX,centerY,actualWidth,actualHeight) sceneGroup:insert(BG) local score = 0 local scoredisplay = display.newText(score,0,0,"comic.ttf",30) scoredisplay.x = centerX + actualWidth1-20 scoredisplay.y = centerY + actualHeight1-display.actualContentHeight+30 sceneGroup:insert(scoredisplay) jelly = {} badjelly = {} i = 1 b = 1 jellytable = {"Jelly (1).png","Jelly (2).png","Jelly (3).png","Jelly (4).png","Jelly (5).png"} function spawnjelly( event ) jelly[i] = display.newImage(jellytable[math.random(5)],math.random(0,actualWidth1),math.random(0,actualHeight1)) jelly[i]:scale(0.4,0.4) print("spawning jelly!") function removeit(event) display.remove(jelly[i]) jelly[i] = nil score = score + 1 scoredisplay.text = score + 1 end function remove1( ... ) jelly[i]:removeEventListener("tap",removeit) end jelly[i]:addEventListener("tap",removeit) jelly[i].value = i i= i + 1 end timer1 = timer.performWithDelay( math.random(500,2000), spawnjelly,0 ) function spawnbadjelly(event) badjelly[b] = display.newImage("jelly (6).png",math.random(0,actualWidth1),math.random(0,actualHeight1)) badjelly[b]:scale(0.4,0.4) function remove2( ... ) badjelly[b]:removeEventListener("tap",lose) end badjelly[b]:addEventListener("tap",lose) badjelly[b].value = b b=b+1 end timer2 = timer.performWithDelay( math.random(500,2000), spawnbadjelly,0 ) local function removeTables() timer.performWithDelay(1, function() for k,v in pairs( jelly ) do display.remove( v ) end jelly = {} end ) timer.performWithDelay(1, function() for k,v in pairs( badjelly ) do display.remove( v ) end badjelly = {} end ) end function lose(event) print("You lose") timer.cancel(timer1) timer.cancel(timer2) removeTables() remove1() remove2() composer.gotoScene("lose") composer.removeScene("game") end end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then 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 end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Hey! Try this in your removeit function.

function removeit(event) removeJ = event.target display.remove(removeJ) score = score + 1 scoredisplay.text = score + 1 end

–SonicX278

 function removeit(event) display.remove(jelly[i]) jelly[i] = nil score = score + 1 scoredisplay.text = score + 1 end function remove1( ... ) jelly[i]:removeEventListener("tap",removeit) end

You don’t need to remove the listener for tap/touch events on objects that you’re removing. Once you remove the object, there is nothing to touch or tap any more.  So you don’t need toe “remove1” function at all.

Rob

Hey! Try this in your removeit function.

function removeit(event) removeJ = event.target display.remove(removeJ) score = score + 1 scoredisplay.text = score + 1 end

–SonicX278

 function removeit(event) display.remove(jelly[i]) jelly[i] = nil score = score + 1 scoredisplay.text = score + 1 end function remove1( ... ) jelly[i]:removeEventListener("tap",removeit) end

You don’t need to remove the listener for tap/touch events on objects that you’re removing. Once you remove the object, there is nothing to touch or tap any more.  So you don’t need toe “remove1” function at all.

Rob