Remove touch event listener after touch

Hi I’m a bit stuck on why this code is not working.

I keep getting the error: ERROR: nil key supplied for property lookup.

My code looks a bit like this:

[code]
local counter = 0;
local count = 5;

local buttonListener = function( event )
if “ended” == event.phase then
counter = counter + 1;
event.target:removeEventListener( “touch”, buttonListener )
print( “addToCounter-”…counter )
if (counter == count) then
print( “Finished” )
counter = 0
Runtime:dispatchEvent({name=counterReached})
end
– we handled it so return true to stop propagation
return true
end
end

local bubble = display.newImage( “images/bubble.png” )
– connect buttonListener
bubble:addEventListener( “touch”, buttonListener )
[/code] [import]uid: 91921 topic_id: 19010 reply_id: 319010[/import]

hmm… I think buttonListener doesn’t exist in the scope of buttonListener… [import]uid: 70635 topic_id: 19010 reply_id: 73253[/import]

Try defining your buttonListener in one of the following ways…
[lua]local function buttonListener( event )[/lua]
or[lua]local buttonListener
buttonListener = function( event )[/lua] [import]uid: 64174 topic_id: 19010 reply_id: 73257[/import]

Thanks it appears this second way you posted is indeed now working. It seems odd to me that I can’t declare and instantiate it all in one go. I’m fairly new to Lua so still working out a few of these scoping issues.

[import]uid: 91921 topic_id: 19010 reply_id: 73332[/import]