Runtime:dispatchEvent( event ) to unknown button

I want to dispatch a touch event to the whole device and activating any button (created with ui.lua)

I do this

local event = { name="touch" , type="touch" , id = touch\_id , phase="began" , time=0 , x=160 , xStart=160 , y=240 , yStart=240 }  
Runtime:dispatchEvent( event )  

I can capture the event using

local function processTouch( event )  
  
 local touch\_id = tostring( event.id )  
 local touch\_phase = tostring( event.phase )  
 local touch\_time = tostring( event.time )  
 local touch\_x = tostring( event.x )  
 local touch\_xStart = tostring( event.xStart )  
 local touch\_y = tostring( event.y )  
 local touch\_yStart = tostring( event.yStart )  
  
 print( touch\_id )  
  
end  
Runtime:addEventListener( "touch" , processTouch )  
  

My issue is that any button which has a self registered touch event doesnt pick up the touch event. How can I include these buttons in the above dispatch event? [import]uid: 5354 topic_id: 4850 reply_id: 304850[/import]

Hi Matthew,

how I understand the documentation is that you can only dispatch events to objects, which kinda makes it very limited.

http://developer.anscamobile.com/reference/index/objectdispatchevent

I don’t think there is a Runtime:DispatchEvent function.

Michael [import]uid: 5712 topic_id: 4850 reply_id: 15616[/import]

Hopefully I can get them to add something.

Then I can do remote touch to go along with the remote accelerometer im doing. www.coronaremote.com [import]uid: 5354 topic_id: 4850 reply_id: 15617[/import]

Nice project you have there. I hope it will pay off for you.

And turn the network code into a module. I am interested in that. [import]uid: 5712 topic_id: 4850 reply_id: 15619[/import]

Matthew, did you have any luck with this? I’m running up against the same problem. I cannot see why a touch event would not be sent to all objects when other types are. m [import]uid: 8271 topic_id: 4850 reply_id: 30270[/import]

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]

eg making these amends for example

[lua]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("runtime fake touch "…tostring(event.target.me))
end
end

circle1=display.newCircle(100,100,50)
circle1:setFillColor(255,0,0,255)
circle1.me = “circle1”
registerEvent(“faketouch”, circle1, circleTouch)
circle1:addEventListener(“touch”, circleTouch)
circle2=display.newCircle(100,200,50)
circle2:setFillColor(255,0,0,255)
circle2.me = “circle2”
registerEvent(“faketouch”, circle2, circleTouch)
circle2:addEventListener(“touch”, circleTouch)

circle3=display.newCircle(100,300,50)
circle3:setFillColor(255,0,0,255)
circle3.me = “circle3”
registerEvent(“faketouch”, circle3, circleTouch)
circle3:addEventListener(“touch”, circleTouch)

local event = { name=“faketouch” , type=“faketouch” , id = touch_id , phase=“fake” , time=0 , x=160 , xStart=160 , y=240 , yStart=240}
Runtime:dispatchEvent( event )[/lua]

now all circles respond to “faketouch” runtime event, but only individually to their own real touch event [import]uid: 6645 topic_id: 4850 reply_id: 30291[/import]