Intent of dispatchEvent?

When a touch event is received it can be stopped from being propagated any further by the listener function returning ‘true’. This does not appear to be the same for events dispatched manually via the ‘object.dispatchEvent’ function.

For example, the following will print two ‘fire’ statements to the console, not one, when I believe that it should only print one because the first display object’s event listener returns ‘true’.

Has this behaviour changed or is this the intent? It would seem to me that the cancellation of event propagation would be useful if it behaved the same in both scenarios.

[lua]local a = display.newCircle(100,100,50)
local b = display.newCircle(300,100,50)

function a:fire(e)
print(a, e.name, e.target)
return true – this should cancel the event IMHO
end

function b:fire(e)
print(b, e.name, e.target)
return true
end

Runtime:addEventListener(“fire”, a)
Runtime:addEventListener(“fire”, b)

Runtime:dispatchEvent{ name=“fire”, target=a, }[/lua] [import]uid: 8271 topic_id: 35301 reply_id: 335301[/import]

I wasn’t even aware you could use object:dispatchEvent in that way through Runtime. The code makes sense but just the way it shortcuts…*brainfreeze*

Anyway, in this case event.target is purely an optional construct so my guess is that all functions listening for “fire” get it more or less simultaneously, and that return true is only meant to prevent propagation through stage layers. But maybe CL has a more technical answer. [import]uid: 41884 topic_id: 35301 reply_id: 140334[/import]

I’m going to ask the team and see if there is a definitive answer, but I’m going to take a stab at this…

Runtime listeners are just functions attached to the runtime, they are not object listeners. So when you do:

Runtime:addEventListener("fire", a)  
Runtime:addEventListener("fire", b)  

It’s going to send an event to the runtime and run a’s fire function. Then it’s going to send a 2nd event to the run time running b’s fire function. There is nothing for a’s fire event to propogate too since the Runtime isn’t an object. It probably successfully stopped it, but you called a 2nd instance of it and I’m guessing that passing the target is just identifying the target, but it’s not telling dispatchEvent where to go.

[import]uid: 199310 topic_id: 35301 reply_id: 140405[/import]

I thought that addEventListener would just add the second argument to a table within the Runtime object. Then, when Runtime:dispatchEvent is called, it simply loops over the objects in the list, calling the listener function, until one of them returns false? [import]uid: 8271 topic_id: 35301 reply_id: 140411[/import]

I wasn’t even aware you could use object:dispatchEvent in that way through Runtime. The code makes sense but just the way it shortcuts…*brainfreeze*

Anyway, in this case event.target is purely an optional construct so my guess is that all functions listening for “fire” get it more or less simultaneously, and that return true is only meant to prevent propagation through stage layers. But maybe CL has a more technical answer. [import]uid: 41884 topic_id: 35301 reply_id: 140334[/import]

I’m going to ask the team and see if there is a definitive answer, but I’m going to take a stab at this…

Runtime listeners are just functions attached to the runtime, they are not object listeners. So when you do:

Runtime:addEventListener("fire", a)  
Runtime:addEventListener("fire", b)  

It’s going to send an event to the runtime and run a’s fire function. Then it’s going to send a 2nd event to the run time running b’s fire function. There is nothing for a’s fire event to propogate too since the Runtime isn’t an object. It probably successfully stopped it, but you called a 2nd instance of it and I’m guessing that passing the target is just identifying the target, but it’s not telling dispatchEvent where to go.

[import]uid: 199310 topic_id: 35301 reply_id: 140405[/import]

I thought that addEventListener would just add the second argument to a table within the Runtime object. Then, when Runtime:dispatchEvent is called, it simply loops over the objects in the list, calling the listener function, until one of them returns false? [import]uid: 8271 topic_id: 35301 reply_id: 140411[/import]