Hi guys. I contact you cause i’ve a big problem with a listener touch. I want realize a memory games. In this function there is a listener touch that i can’t remove. When i press the button back to return in main menu the listener remain active. How i can do? I tried remove listener in button back, but i had error.
--Set up game function function game(object, event) if(event.phase == "began") then if(checkForMatch == false and secondSelect == 0) then --Flip over first button buttonCover[object.number].isVisible = false; lastButton = object checkForMatch = true elseif(checkForMatch == true) then if(secondSelect == 0) then --Flip over second button buttonCover[object.number].isVisible = false; secondSelect = 1; --If buttons do not match, flip buttons over if(lastButton.myName ~= object.myName) then matchText.text = "Match Not Found!"; timer.performWithDelay(1250, function() matchText.text = " "; checkForMatch = false; secondSelect = 0; buttonCover[lastButton.number].isVisible = true; buttonCover[object.number].isVisible = true; end, 1) --If buttons DO match, remove buttons elseif(lastButton.myName == object.myName) then matchText.text = "Match Found!"; timer.performWithDelay(1250, function() matchText.text = " "; checkForMatch = false; secondSelect = 0; lastButton:removeSelf(); object:removeSelf(); buttonCover[lastButton.number]:removeSelf(); buttonCover[object.number]:removeSelf(); end, 1) end end end end end --Place buttons on screen for count = 1,3 do x = x + 90 y = 20 for insideCount = 1,4 do y = y + 90 --Assign each image a random location on grid temp = math.random(1,#buttonImages) button[count] = display.newImage(buttonImages[temp] .. ".png"); --Position the button button[count].x = x; button[count].y = y; --Give each a button a name button[count].myName = buttonImages[temp] button[count].number = totalButtons --Remove button from buttonImages table table.remove(buttonImages, temp) --Set a cover to hide the button image buttonCover[totalButtons] = display.newImage("button.png"); buttonCover[totalButtons].x = x; buttonCover[totalButtons].y = y; totalButtons = totalButtons + 1 --Attach listener event to each button button[count].touch = game button[count]:addEventListener( "touch", button[count] ) end end