have a look at this maybe
http://developer.anscamobile.com/forum/2011/03/04/global-local-event-forwarding-table-listener
or this
http://developer.anscamobile.com/forum/2010/12/15/generically-dispatch-system-events-your-object-event-listeners
I knocked it up into a helper function, which’ll respond to Runtime dispatches, but also note every object responds to a normal touch as well. (ie if i touch circle1 it fires it’s touch event, but then also fires for circle1, circle2 and circle3 again)
maybe you can work on these principles though to get something to work. basically you’re registering a list of objects with the event/listener and then looping through
[lua]objectRuntimeEventDispatcher = function(self, event)
– Usage for “enterFrame” events with a display object “ob”:
– ob[enterFrame] = caEventHandlers.objectRuntimeEventDispatcher
– Register object-table event handler with Runtime like:
– Runtime:addEventListener( “enterFrame”, ob )
– will then forward “enterFrame” event to object’s event handlers like
– “myEnterFrameHandler(event)” that have been registered like:
– ob:addEventListener( “enterFrame”, myEnterFrameHandler )
if (not self.dispatchEvent) then
– poor-man’s test for display object existence
– if we’re here, then only skeleton object exists and we should clean-up
Runtime:removeEventListener( event.name, self )
return true
else
– dispatch event to object itself
– we cannot annotate and reuse the event table as bad things will happen
– so we have to copy the event table for dispatching
local ev = {}
for k,v in pairs(event)do
ev[k]=v
end
– add self as the target such that the handler can find the self-state
ev.target = self
self:dispatchEvent(ev)
return false
end
end
local function registerEvent(eventName, obj, fn)
– object listener
obj[eventName] = objectRuntimeEventDispatcher
obj:addEventListener( eventName, fn )
– pass through from runtime
Runtime:addEventListener( eventName, obj )
end
local circle1
local circle2
local circle3
local function circleTouch(event)
if(event.phase==“began”) then
print("touched: "…tostring(event.target.me))
elseif(event.phase==“fake”) then
print("fake runtime touch: "…tostring(event.target.me))
end
end
circle1=display.newCircle(100,100,50)
circle1:setFillColor(255,0,0,255)
circle1.me = “circle1”
registerEvent(“touch”, circle1, circleTouch)
circle2=display.newCircle(100,200,50)
circle2:setFillColor(255,0,0,255)
circle2.me = “circle2”
registerEvent(“touch”, circle2, circleTouch)
circle3=display.newCircle(100,300,50)
circle3:setFillColor(255,0,0,255)
circle3.me = “circle3”
registerEvent(“touch”, circle3, circleTouch)
local event = { name=“touch” , type=“touch” , id = touch_id , phase=“fake” , time=0 , x=160 , xStart=160 , y=240 , yStart=240}
Runtime:dispatchEvent( event )[/lua]
[import]uid: 6645 topic_id: 4850 reply_id: 30285[/import]