Use of removeEventListener

I seem to be having some problems with using removeEventListener. In the code below, I would expect count to go from 10 down to 0, then stop. For me, using build 243, it just keeps going down meaning that the event listener has not been removed.

count = 10  
  
local gameLoop = function()  
 count = count - 1  
 if count \< 1 then  
 Runtime:removeEventListener( "enterFrame", self )  
 end  
 print(count)  
end  
  
Runtime:addEventListener( "enterFrame", gameLoop )  

How should I remove event listeners properly? [import]uid: 12474 topic_id: 4918 reply_id: 304918[/import]

“self” is not defined in a local function. So calling removeEventListener on “self” does nothing. Remove the same listener you added, gameLoop. [import]uid: 12108 topic_id: 4918 reply_id: 15893[/import]

Thanks for your answer jhocking. I’ve tried changing that line from self to gameLoop but I get exactly the same result. Any further suggestions?

[code]
count = 10

local gameLoop = function()
count = count - 1
if count < 1 then
Runtime:removeEventListener( “enterFrame”, gameLoop )
end
print(count)
end

Runtime:addEventListener( “enterFrame”, gameLoop )
[/code] [import]uid: 12474 topic_id: 4918 reply_id: 15920[/import]

does this make any difference? (untested)

[lua]count = 10
local gameLoop

function gameLoop() – still local due to line above
count = count - 1
if count < 1 then
– might see gameLoop now, because pre-defined
Runtime:removeEventListener( “enterFrame”, gameLoop )
end
print(count)
end

Runtime:addEventListener( “enterFrame”, gameLoop )[/lua] [import]uid: 6645 topic_id: 4918 reply_id: 15922[/import]

Yes, that works. Thanks jmp909! [import]uid: 12474 topic_id: 4918 reply_id: 15923[/import]

Interesting. That syntax looks fine to me, but apparently not.

First I tried removing the keyword “local” and that worked.

Then because I figured it should work as a local function, I rearranged the words and it worked:

local function gameLoop()  

“function name()” is how I generally write functions, not “name = function()” but I thought they were really the same and it’s just a personal preference. Live and learn.

EDIT: wrote this before seeing his post. So apparently writing “function name()” pre-defines the function but “name = function()” does not. That’s a reason for me to just keep doing what I already am. [import]uid: 12108 topic_id: 4918 reply_id: 15924[/import]

@jhocking: I agree. No need for shenanigans. I just do function declarations the old fashion way unless there is a reason to declare the function as a variable. [import]uid: 11393 topic_id: 4918 reply_id: 15926[/import]

I’m just learning Corona and Lua at the moment, and used “name = function()” because that’s what is used in the particular sample of code that I’m using to learn from.

Having read your comment, I’ve noticed that the Corona documentation generally uses “function name()”. As this notation is a bit clearer for me, I’ll switch to using that from now on. [import]uid: 12474 topic_id: 4918 reply_id: 15927[/import]

@skafta: Totally understand, and my comment wasn’t directed at you specifically. I was in the same boat as you not so long ago.

The samples sometimes use that form of function declaration, and I’m sure there is a reason why you would need to do so, but I’ve not had to so far.

I just like sticking to what I know - one less thing to break in the code :wink: [import]uid: 11393 topic_id: 4918 reply_id: 15930[/import]

one thing, in case i hadn’t made it clear:
[lua]local gameLoop
function gameLoop[/lua]

is essentially the same as writing

[lua]local function gameLoop[/lua]

except the forward reference in the first example means the function can see it’s own definition [import]uid: 6645 topic_id: 4918 reply_id: 16163[/import]

OK, I really hoped this thread solved this hair-pulling problem I have. My runtime:addEventListener() cannot be removed. I had the syntax talked about above:

local blah = function ()

I changed it back to what I normally use:

local function blah()

but my "runtime:removeEventListener(“enterFrame”, sendPlane) is not removing the previous addEventListener. It just keeps calling sendPlane constantly.

I don’t know if this is a side-effect of using DIRECTOR class, but I seriously doubt it. It leads me to believe that “removeEventListener” is broken.

I’m setting a counter on the addEventListener and then deducting when I remove it, and so I’m fairly confident that the EventListener isn’t being added multiple times.

NOTE: what is interesting is that my UI button is causing me to removeEventListener twice for some reason. It must be on “touch” and so it calls when I press the button, then calls it again when I lift off the screen. I’m assuming removeEventListener() being called redundantly doesn’t do anything if it has been removed already (although, it’s not removing it - which is sorta the point… not the first time it’s called, nor the second time either)

SOLUTION: My issue was a scope deal; my addEventListener( ) was referring to a local function and my removeEventListener( ) was being invoked at a level deeper, so it wasn’t working. Once my function that was being called BY the EventListener( ) was no longer a local function, I could then removeEventListener() successfully. I share this in case someone else burns 3 hours like I did. [import]uid: 74844 topic_id: 4918 reply_id: 50329[/import]

thanks jerome8! that worked for me too. [import]uid: 9546 topic_id: 4918 reply_id: 54277[/import]

Many thanks, jmp909 and jerome82! I was driving myself crazy with the same issue, and forward referencing was definitely the solution! [import]uid: 14598 topic_id: 4918 reply_id: 85664[/import]

This above fix still doesn’t work for me. Can someone please assist?

Here is my function that is being called.

Thanks
-J

[code]
local function initGameSelectSigns(bool)

local gameSelectListener

function gameSelectListener(event)

print(“removing listener after click”)
print(" ")

Runtime:removeEventListener( “gameSelectSignHit”, gameSelectListener )

– this kills the displayGroup
gameSelectSigns.kill()

– code here gets called multiple times if I add the listener multiple times even though im calling Runtime:removeEventListener

end

if bool ~= false then

print(“adding listener when panning right”)
print(" ")

– this returns a displayGroup
frontLayerGroup:insert(gameSelectSigns.new(1100, _H - 60))

Runtime:addEventListener( “gameSelectSignHit”, gameSelectListener )

else

print(“removing listener when panning left”)
print(" ")

Runtime:removeEventListener( “gameSelectSignHit”, gameSelectListener )

– this kills the displayGroup
gameSelectSigns.kill()

end

end
[/code] [import]uid: 21584 topic_id: 4918 reply_id: 102958[/import]

@jayru_13,

“gameSelectSignHit” that you use in your Runttime:addEventListener is not a valid runtime event. Corona only understands a small set of listener events: “touch”, “tap:”, “enterFrame”, etc. Your listener will never be called. [import]uid: 7559 topic_id: 4918 reply_id: 103072[/import]

Hi Tom. Actually the event fires just fine. My only problem is that I cant remove the listener after it initially gets added.

-J [import]uid: 21584 topic_id: 4918 reply_id: 103073[/import]

Just as a quick test, try changing the event type to “touch” or “tap” and see if the listener can be removed.

How are you defining “gameSelectSignHit”? I didn’t see that in your code. [import]uid: 7559 topic_id: 4918 reply_id: 103074[/import]

In another script I m calling…

local event = {name = “gameSelectSignHit”, id = event.target.id}
Runtime:dispatchEvent(event)

Ill try the test and see if thats the issue. If so, can I dispatch custom events on a display object instead?

-J [import]uid: 21584 topic_id: 4918 reply_id: 103075[/import]

I just tested it with event type “tap” and it still doesn’t get removed.

Thanks for your help
-J [import]uid: 21584 topic_id: 4918 reply_id: 103076[/import]

Just FYI. According to this page’s documentation I can register custom Runtime events

http://developer.anscamobile.com/reference/index/objectdispatchevent

-J [import]uid: 21584 topic_id: 4918 reply_id: 103078[/import]