Is it possible to test whether an object is listening for an event? For example, if a group passed into a function is listening for tap events.
Well, if you set up your listeners like this:
local function onTap( self, event ) end local myGroup = display.newGroup() for i = 1, 10 do local obj = display.newCircle( myGroup, 10 \* i, 10 \* i, 5 ) -- randomly add tap listener to objects. if( math.random(1,2) == 1) then obj.tap = onTap obj:addEventListener( "tap" ) end end
Then, you can simply check for the presence of ‘tap’ on the object and assume that means it is listening.
-- Check for tap listeners and remove for i = 1, myGroup.numChildren do if( myGroup[i].tap ) then print( i, " was listening for tap. Removing...") myGroup[i]:removeEventListener( "tap" ) myGroup[i].tap = nil end end
By the end of the sample code above, you’ve detected the objects that are listening and removed/cleared them so they won’t be seen as listening if checked again later.
Additionally, I think you can dig into objects like this:
-
Checking if obj is using a ‘touch’ table listener:
if( obj._tableListeners[“touch”] ) then print( “listening for ‘touch’” ) end
-
Checking if obj is using a ‘touch’ runtime (function) listener:
if( obj._functionListeners[“touch”] ) then print( “listening for ‘touch’” ) end
I’m sure the tables exist (_functionListeners and _tableListeners), but I’m not 100% sure about the structure of the contents of those tables.
You may want to dump them. If you have SSK2, you can do this:
table.print\_r( obj.\_functionListeners ) table.print\_r( obj.\_tableListeners)
or this if print_r crashes:
-- SSK2 2017.010 or later table.xinspect( obj.\_functionListeners ) table.xinspect( obj.\_tableListeners)
I’d like to know if the addEventListener function has been called to attach a particular event, rather than particular event handler, onto an object. The problem being that the handler function does not need to be called, in this case, ‘tap’. I could override addEventListener, but doing that involves overriding all the display.newXxx functions to add the modified function to every display object.
I think I answered that in my second post.
Tip: When I post responses, I usually go through a few edits and even additional posts. So, if you see that I just answered, give me a few more minutes to clean it up and re-format, plus add any other things that occur to me.
Sorry for the delay in my response-style, but that’s just the way I think.
Nice one. That hadn’t appeared when I posted. Post clash!
Well, if you set up your listeners like this:
local function onTap( self, event ) end local myGroup = display.newGroup() for i = 1, 10 do local obj = display.newCircle( myGroup, 10 \* i, 10 \* i, 5 ) -- randomly add tap listener to objects. if( math.random(1,2) == 1) then obj.tap = onTap obj:addEventListener( "tap" ) end end
Then, you can simply check for the presence of ‘tap’ on the object and assume that means it is listening.
-- Check for tap listeners and remove for i = 1, myGroup.numChildren do if( myGroup[i].tap ) then print( i, " was listening for tap. Removing...") myGroup[i]:removeEventListener( "tap" ) myGroup[i].tap = nil end end
By the end of the sample code above, you’ve detected the objects that are listening and removed/cleared them so they won’t be seen as listening if checked again later.
Additionally, I think you can dig into objects like this:
-
Checking if obj is using a ‘touch’ table listener:
if( obj._tableListeners[“touch”] ) then print( “listening for ‘touch’” ) end
-
Checking if obj is using a ‘touch’ runtime (function) listener:
if( obj._functionListeners[“touch”] ) then print( “listening for ‘touch’” ) end
I’m sure the tables exist (_functionListeners and _tableListeners), but I’m not 100% sure about the structure of the contents of those tables.
You may want to dump them. If you have SSK2, you can do this:
table.print\_r( obj.\_functionListeners ) table.print\_r( obj.\_tableListeners)
or this if print_r crashes:
-- SSK2 2017.010 or later table.xinspect( obj.\_functionListeners ) table.xinspect( obj.\_tableListeners)
I’d like to know if the addEventListener function has been called to attach a particular event, rather than particular event handler, onto an object. The problem being that the handler function does not need to be called, in this case, ‘tap’. I could override addEventListener, but doing that involves overriding all the display.newXxx functions to add the modified function to every display object.
I think I answered that in my second post.
Tip: When I post responses, I usually go through a few edits and even additional posts. So, if you see that I just answered, give me a few more minutes to clean it up and re-format, plus add any other things that occur to me.
Sorry for the delay in my response-style, but that’s just the way I think.
Nice one. That hadn’t appeared when I posted. Post clash!