changeMyListener("remove") doesn't work sometimes

Hi All, 

I have a problem that sometimes when Game Over, I want to change all Event Listener to be removed, but sometimes not sure why, when Game Over event is starting (the image is becoming big), the event listeners are not removed yet. Any opinion will be highly appreciated!

My code is below:

local changeMyListener local gameOver = function() timer.cancel(stopCountdown) changeMyListener("remove") transition.to(ausH,{time=7000,xScale=1.8,yScale=1}) transition.to(asiaH,{time=7000,xScale=1.8,yScale=1}) Runtime:addEventListener("touch", leave) end) end local changeTimeLabel = function() if remainVampire == 0 then movingWords.text = "You loss!" gameOver() end end changeMyListener = function(mode) if mode == "add" then asia:addEventListener("touch",PAsia) europe:addEventListener("touch",PEurope) aus:addEventListener("touch",PAus) Africa:addEventListener("touch",PAfrica) NAmerica:addEventListener("touch",PNAmerica) SAmerica:addEventListener("touch",PSAmerica) elseif mode =="remove" then asia:removeEventListener("touch",PAsia) europe:removeEventListener("touch",PEurope) aus:removeEventListener("touch",PAus) Africa:removeEventListener("touch",PAfrica) NAmerica:removeEventListener("touch",PNAmerica) SAmerica:removeEventListener("touch",PSAmerica) end end

Well, we can only theorize, but there are a few things you should check.

  1. Add a print statement into your changeMyListener function, to see if the function is really executed. Maybe elt it return “mode” as well.

  2. I assume changeMyListener is intialized somewhere at the top? (local changeMyListener)

These are the first things that come in mind you should check.

Greetings

Torben :slight_smile:

Hi torbenratzlaff, thanks for your quick reply!

  1. I am totally newbie, what the print statement should be?

  2. yes, It is already at the top.

Thanks a lot!

Cheers,

stone_hhk

For the print statement, I think that torbenratzlaff just means you should add some print statements to help you debug. Have them print anything that could be useful: 

changeMyListener = function(mode) print("changeMyListener has been called") print("mode = ", mode) if mode == "add" then print("in the add section") print("1") asia:addEventListener("touch",PAsia) print("2") europe:addEventListener("touch",PEurope) print("3") aus:addEventListener("touch",PAus) print("4") Africa:addEventListener("touch",PAfrica) print("5") NAmerica:addEventListener("touch",PNAmerica) print("6") SAmerica:addEventListener("touch",PSAmerica) print("7") elseif mode =="remove" then print("in the remove section") print("1") asia:removeEventListener("touch",PAsia) print("2") europe:removeEventListener("touch",PEurope) print("3") aus:removeEventListener("touch",PAus) print("4") Africa:removeEventListener("touch",PAfrica) print("5") NAmerica:removeEventListener("touch",PNAmerica) print("6") SAmerica:removeEventListener("touch",PSAmerica) print("7") end print("ending the changeMyListener function") end

This is a bit crude, but now when changeMyListener(“remove”) gets called, you should see the following printed in the terminal:

changeMyListener has been called mode = remove 1 2 3 4 5 6 7 ending the changeMyListener function

If you don’t then at least you can work out where it is going wrong. For instance maybe it will say “mode = nil” or “mode  = add”, or it will print 1 and 2 but not 3-7 because there is a runtime error.

Print statement are a valuable tool for finding out what is actually going on in your code (really you should use the Corona Debugger for this, but prints are good to use too).

The global “print” function displays text in the simulators console. It’s mainly used for debugging.

Use it to check when the error is happening.

https://docs.coronalabs.com/api/library/global/print.html

EDIT:

Alan made it quite clear already.

Thanks both Alan QuizTix and torbenratzlaff!

the log is as below, not sure why, even remove section seems has be done, but did not remove the listener at the end, and it is called again, when I click on the screen.

6** 月 **** 09 11:56:16.872 c**hangeMyListener has been called

6** 月 **** 09 11:56:16.873 m**ode = remove

6** 月 **** 09 11:56:16.873 i**n the remove section

6** 月 **** 09 11:56:16.889 8**

6** 月 **** 09 11:56:16.889 9**

6** 月 **** 09 11:56:16.889 1**0

6** 月 **** 09 11:56:16.890 1**1

6** 月 **** 09 11:56:16.890 1**2

6** 月 **** 09 11:56:16.890 1**3

6** 月 **** 09 11:56:16.890 1**4

6** 月 **** 09 11:56:16.890 e**nding the changeMyListener function

6** 月 **** 09 11:56:26.377 c**hangeMyListener has been called

6** 月 **** 09 11:56:26.388 m**ode = remove

6** 月 **** 09 11:56:26.388 i**n the remove section

6** 月 **** 09 11:56:26.388 8**

6** 月 **** 09 11:56:26.388 E**RROR: Runtime error
/Users/rahab/Desktop/Vampire War/simple.lua:1136: attempt to call method ‘removeEventListener’ (a nil value)
stack traceback:
/Users/rahab/Desktop/Vampire War/simple.lua:1136: in function ‘changeMyListener’
/Users/rahab/Desktop/Vampire War/simple.lua:633: in function ‘_listener’
?: in function <?:167>
?: in function <?:205>

Well, we can only theorize, but there are a few things you should check.

  1. Add a print statement into your changeMyListener function, to see if the function is really executed. Maybe elt it return “mode” as well.

  2. I assume changeMyListener is intialized somewhere at the top? (local changeMyListener)

These are the first things that come in mind you should check.

Greetings

Torben :slight_smile:

Hi torbenratzlaff, thanks for your quick reply!

  1. I am totally newbie, what the print statement should be?

  2. yes, It is already at the top.

Thanks a lot!

Cheers,

stone_hhk

For the print statement, I think that torbenratzlaff just means you should add some print statements to help you debug. Have them print anything that could be useful: 

changeMyListener = function(mode) print("changeMyListener has been called") print("mode = ", mode) if mode == "add" then print("in the add section") print("1") asia:addEventListener("touch",PAsia) print("2") europe:addEventListener("touch",PEurope) print("3") aus:addEventListener("touch",PAus) print("4") Africa:addEventListener("touch",PAfrica) print("5") NAmerica:addEventListener("touch",PNAmerica) print("6") SAmerica:addEventListener("touch",PSAmerica) print("7") elseif mode =="remove" then print("in the remove section") print("1") asia:removeEventListener("touch",PAsia) print("2") europe:removeEventListener("touch",PEurope) print("3") aus:removeEventListener("touch",PAus) print("4") Africa:removeEventListener("touch",PAfrica) print("5") NAmerica:removeEventListener("touch",PNAmerica) print("6") SAmerica:removeEventListener("touch",PSAmerica) print("7") end print("ending the changeMyListener function") end

This is a bit crude, but now when changeMyListener(“remove”) gets called, you should see the following printed in the terminal:

changeMyListener has been called mode = remove 1 2 3 4 5 6 7 ending the changeMyListener function

If you don’t then at least you can work out where it is going wrong. For instance maybe it will say “mode = nil” or “mode  = add”, or it will print 1 and 2 but not 3-7 because there is a runtime error.

Print statement are a valuable tool for finding out what is actually going on in your code (really you should use the Corona Debugger for this, but prints are good to use too).

The global “print” function displays text in the simulators console. It’s mainly used for debugging.

Use it to check when the error is happening.

https://docs.coronalabs.com/api/library/global/print.html

EDIT:

Alan made it quite clear already.

Thanks both Alan QuizTix and torbenratzlaff!

the log is as below, not sure why, even remove section seems has be done, but did not remove the listener at the end, and it is called again, when I click on the screen.

6** 月 **** 09 11:56:16.872 c**hangeMyListener has been called

6** 月 **** 09 11:56:16.873 m**ode = remove

6** 月 **** 09 11:56:16.873 i**n the remove section

6** 月 **** 09 11:56:16.889 8**

6** 月 **** 09 11:56:16.889 9**

6** 月 **** 09 11:56:16.889 1**0

6** 月 **** 09 11:56:16.890 1**1

6** 月 **** 09 11:56:16.890 1**2

6** 月 **** 09 11:56:16.890 1**3

6** 月 **** 09 11:56:16.890 1**4

6** 月 **** 09 11:56:16.890 e**nding the changeMyListener function

6** 月 **** 09 11:56:26.377 c**hangeMyListener has been called

6** 月 **** 09 11:56:26.388 m**ode = remove

6** 月 **** 09 11:56:26.388 i**n the remove section

6** 月 **** 09 11:56:26.388 8**

6** 月 **** 09 11:56:26.388 E**RROR: Runtime error
/Users/rahab/Desktop/Vampire War/simple.lua:1136: attempt to call method ‘removeEventListener’ (a nil value)
stack traceback:
/Users/rahab/Desktop/Vampire War/simple.lua:1136: in function ‘changeMyListener’
/Users/rahab/Desktop/Vampire War/simple.lua:633: in function ‘_listener’
?: in function <?:167>
?: in function <?:205>