local test = display.newRact(10,10,100,100)
test:setFillColor(255,0,0)
local handleTouch = function ( event )
if "ended" == event.phase then
print ("This one time only")
end
test:removeEventListener ( "touch", handleTouch )
end
test:addEventListener ( "touch", handleTouch )
Can we have a eventListener negator?
To remove an eventListener the function, etc has to be known or be the same. why can’t it just be a {} to remove/disable the event handler or a nil.
so instead of removeEventListener, it would be like
test:setEventListener ( touch,nil )
or
test:setEventListener ( "touch", {} )
which is much easier and this way it can be deactivated from the calling event itself and not cause crashes.
note: you’re trying to remove your handler 3 times (it’s called in every phase). but that’s not the problem… your function needs to see itself so you need to predefine it with a forward reference
[lua]local test = display.newRect(10,10,100,100)
test:setFillColor(255,0,0)
local handleTouch – forward reference
function handleTouch(event) – still actually local
print(“touch:”…event.phase)
if “ended” == event.phase then
print (“This one time only”)
test:removeEventListener ( “touch”, handleTouch ) – can see local reference now
end
end