Removing of eventHandlers

Hi,
just wanted to understand the following

say,

1
2
3
4
5
6
7
8
9
10
11
12
 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 )

This is VERY interesting. Have you done any testing to verify that this works?

@jon,
the second part was a suggestion, not the results of an experiment.

cheers,

Jayant C Varma

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
 
 test:addEventListener ( "touch", handleTouch )
views:1394 update:2011/10/5 21:23:48
corona forums © 2003-2011