Corona sdk events dispatched with dispatchEvent() are handled directly upon call. Why so?
- by Amoxus
I noticed to my surprise that an event created with dispatchEvent(event) gets handled directly when called, and not together with other events at a specific phase of the frame loop.
Two main reasons of having an event system are:
so that you can call code B from code A, but still want to prioritize code A.
to make sure there are no freaky loopedy loops where code A calls code B calls code A ...
I wonder what Ansca's rationale behind having events being handled directly this way is. And does Corona handle loopedy loops and other such pitfalls gracefully?
The following code demonstrates dispatchEvent():
T= {}
Z = display.newRect(100,100,100,100)
function T.doSomething()
print("T.doSomething: begun")
local event = { name="myEventType", target=T }
Z:dispatchEvent( event )
print("T.doSomething: ended")
end
function Z.sayHello(event)
print("Z.sayHello: begun and ended")
end
Z:addEventListener("myEventType", Z.sayHello)
print("Main: begun")
T.doSomething()
print("Main: ended")
However Ansca claims the contrary at http://developer.coronalabs.com/reference/index/objectdispatchevent
Can anyone clear this up a little?
( Using Corona simulator V 2012.840 )