Trying to figure out how to stop a function

This code keeps calling the print function even though i remove the listener.

[code]local doCollision = function()
print(“has this stopped”)
Runtime:removeEventListener(“enterFrame”, doCollision)
end

Runtime:addEventListener(“enterFrame”, doCollision)[/code]

while this code stops the print function after the first time just fine

[code]local function doCollision()
print(“has this stopped”)
Runtime:removeEventListener(“enterFrame”, doCollision)
end

Runtime:addEventListener(“enterFrame”, doCollision)[/code]
What i have been trying to figure out is how i can stop running the function with the first function. I have tried several things but i cannot seem to figure it out.
[import]uid: 39391 topic_id: 9238 reply_id: 309238[/import]

Well, from the LUA manual…

_Functions are conventionally defined in Lua with the function keyword as follows:

function function_name ( args ) body end

The above syntax is actually just a convenience for the following equivalent syntax:

function_name = function( args ) body end_

My only guess is that if what you are saying is true, the manual is wrong, and that in your first instance doCollision isn’t set until the end of the code block, and in the second, it is set instantly (if undefined until the end of the block)

Which actually makes sense… :slight_smile:
First time, you are setting doCollision to = an anonymous function, that, as doCollision isn’t set yet, has no idea what it is.
My guess is you are removing nil
[import]uid: 34945 topic_id: 9238 reply_id: 33692[/import]

yeah it is very true. you can set just those 5 lines of code up in the simulator, and one of them creates a infinite loop while the other just prints once and stops.

of course i just used the one that works, but i wanted to post because i wanted to know why the other did not work and just created a loop.

I am actually still a little confused by your reply

What i am removing is the event listener which clearly has fired and is not nil once the function has started. [import]uid: 39391 topic_id: 9238 reply_id: 33729[/import]

What you’re noticing is subtle behavior with local scope. Basically, in one case the local function is known within the scope of the function but in the other case the local is not known within the scope.

Personally I prefer the latter syntax anyway (looks more like other programming languages) but if you’re insistent on writing using the first syntax then you can’t use a local function, it must be global. [import]uid: 12108 topic_id: 9238 reply_id: 33740[/import]

Thank you. that explains it. [import]uid: 39391 topic_id: 9238 reply_id: 33756[/import]

Thank you. that explains it. [import]uid: 39391 topic_id: 9238 reply_id: 33757[/import]

somewhere in the manual (i don’t have it with me at present, so i can’t tell you the exact page) it says that if a function is recursive you must declare it using the second syntax.
that’s exactly what you have, the doCollision function calls itself
[import]uid: 6459 topic_id: 9238 reply_id: 33763[/import]

I tried using this method to cancel a tap zoom function and it won’t even zoom the first time. Any ideas?

[code]

local function onTap (event)

if 2 == event.numTaps then
localGroup:scale(1.6,1.6)
end

background:removeEventListener(“tap”, onTap)

end

background:addEventListener(“tap”, onTap) [import]uid: 31005 topic_id: 9238 reply_id: 33973[/import]