Just some clarification on how corona runs

Say I have a button with two event listeners (both for touch events, but linked to two different functions). If I add the first event listener to call on function 1 ( a large function) and then the next line is the second event listener to call on function 2, how does corona see this? Will it run through function 1 to completion before moving on to the second event listener calling on function 2? Or maybe simultaneously while it is running through function 1 will it move on to the event listener calling on function 2?

Sorry if confusing, I was just curious. Thanks for any help!

Hi Nate, this seemed to have gotten posted twice, I’ve removed the duplicate post.

To answer your question, if you run this little snippet and watch the console log:

local r = display.newRect(100,100,100,100) local function b(event)      print("b", event.phase)      return true end local function a(event)      print("a", event.phase)      return true end r:addEventListener("touch", a) r:addEventListener("touch", b)

you will see printed:

2013-11-30 15:41:20.880 Corona Simulator[8163:507] a    began 2013-11-30 15:41:20.881 Corona Simulator[8163:507] b    began 2013-11-30 15:41:20.993 Corona Simulator[8163:507] a    ended 2013-11-30 15:41:20.993 Corona Simulator[8163:507] b    ended

So it runs the first function first then the second function.  But I think it’s important to understand that these are fired asynchronously.  That means it’s going to fire the began phase event to A then immediately fire the event to B.  It’s very possible that if function A takes a while that function B could fire in the middle of A’s running and depending on what all goes on, it’s possible that the B ended event could come before the A ended event.  So you may end up with A began, B began, B ended, A ended.

Rob

Very helpful explanation! Thanks a bunch!

Hi Nate, this seemed to have gotten posted twice, I’ve removed the duplicate post.

To answer your question, if you run this little snippet and watch the console log:

local r = display.newRect(100,100,100,100) local function b(event)      print("b", event.phase)      return true end local function a(event)      print("a", event.phase)      return true end r:addEventListener("touch", a) r:addEventListener("touch", b)

you will see printed:

2013-11-30 15:41:20.880 Corona Simulator[8163:507] a    began 2013-11-30 15:41:20.881 Corona Simulator[8163:507] b    began 2013-11-30 15:41:20.993 Corona Simulator[8163:507] a    ended 2013-11-30 15:41:20.993 Corona Simulator[8163:507] b    ended

So it runs the first function first then the second function.  But I think it’s important to understand that these are fired asynchronously.  That means it’s going to fire the began phase event to A then immediately fire the event to B.  It’s very possible that if function A takes a while that function B could fire in the middle of A’s running and depending on what all goes on, it’s possible that the B ended event could come before the A ended event.  So you may end up with A began, B began, B ended, A ended.

Rob

Very helpful explanation! Thanks a bunch!