How to change order of event listeners of a displayObject

Hello there,

I’m currently creating a new application (again) using Corona, and one thing I’m trying to use more from now  on, are the event Dispatcher functions attached in the display library.

I started to see how interesting it is to set custom triggers to an displayObject I created, and bind them to buttons using “object:dispatchEvent()”.

I’m currently developing a RPG game (or just messing around with code again…), and I’m using these triggers to execute some custom skill function. It’s easy to use them this way because I can simply plug or unplug  them as pieces of LEGO in the code, making it easier to develop the game.

However, with this API, there is just 2 operations I can use:    add       and    remove :

  • As I can see when the application starts, the add function works like inside a stack, adding the  elements at the end of the object. 
  • And the removal operation removes the listener from the object and reorders it, so it doesn’t  trigger any error.

Here is my Question:

Is there a way to manage or access this “stack of listeners” manually from the script?

I wanted to add a desired listener at the beginning of the stack, instead of adding it in the end (without having to keep track of all listeners added). Or I wanted to reorder it based in some comparison operation…

Thanks!

Carlos HR.

Officially, there is no way to guarantee the order in which events are delivered; So no, there is no way to control the ‘stack’ of listeners.

dispatchEvent does not bind anything, it requests that an event object gets passed to any object registered as a listener for the event name.

The event listener remove function does not reorder anything, it removes the registered listener for the event name.

I suggest that you are mixing metaphors because your code is working one way and you’re confusing that with the way the Corona framework manages itself.

If you want to manage the order in which listener functions on your objects are called - or, more correctly, you want to manage the order of priority which those objects have for event listener firing - you need to manage the list of objects and their listener functions yourself.

Hello, you can try this :

local app={}   function app:hello(  )     print(2)   end   app:addEventListener( "hello", app )   app:addEventListener( "hello", function( )     print( 1 )   end )   app:addEventListener( "hello", function( )     print( 3 )   end ) app:dispatchEvent({name="hello"})

That will print 1 then 3 and 2.

(Notice that they are the same result by switching table and functions)

Functions always be on the first time.

You can access that listeners table and functions on 

  app.\_tableListeners   app.\_functionListeners

Then linked to the name of the event : “hello”

  app.\_functionListeners.hello

And then make a simple switch of the 2 functions inside.

  app.\_functionListeners.hello[1],app.\_functionListeners.hello[2]=app.\_functionListeners.hello[2],app.\_functionListeners.hello[1]

And now when you call the event :

app:dispatchEvent({name="hello"}) -- print 3, 1 and then 2

It had been reverse the 2 functions.

Yvan.

Thanks, that solves my problem.

Now I see there’s really a difference when you add a function or a table as an event listener.

OBS:

never thought of doing that:  “a, b = b, a”.

always did this:

    local tmp = a

    a = b

    b = tmp

thanks! :smiley:

Officially, there is no way to guarantee the order in which events are delivered; So no, there is no way to control the ‘stack’ of listeners.

dispatchEvent does not bind anything, it requests that an event object gets passed to any object registered as a listener for the event name.

The event listener remove function does not reorder anything, it removes the registered listener for the event name.

I suggest that you are mixing metaphors because your code is working one way and you’re confusing that with the way the Corona framework manages itself.

If you want to manage the order in which listener functions on your objects are called - or, more correctly, you want to manage the order of priority which those objects have for event listener firing - you need to manage the list of objects and their listener functions yourself.

Hello, you can try this :

local app={}   function app:hello(  )     print(2)   end   app:addEventListener( "hello", app )   app:addEventListener( "hello", function( )     print( 1 )   end )   app:addEventListener( "hello", function( )     print( 3 )   end ) app:dispatchEvent({name="hello"})

That will print 1 then 3 and 2.

(Notice that they are the same result by switching table and functions)

Functions always be on the first time.

You can access that listeners table and functions on 

  app.\_tableListeners   app.\_functionListeners

Then linked to the name of the event : “hello”

  app.\_functionListeners.hello

And then make a simple switch of the 2 functions inside.

  app.\_functionListeners.hello[1],app.\_functionListeners.hello[2]=app.\_functionListeners.hello[2],app.\_functionListeners.hello[1]

And now when you call the event :

app:dispatchEvent({name="hello"}) -- print 3, 1 and then 2

It had been reverse the 2 functions.

Yvan.

Thanks, that solves my problem.

Now I see there’s really a difference when you add a function or a table as an event listener.

OBS:

never thought of doing that:  “a, b = b, a”.

always did this:

    local tmp = a

    a = b

    b = tmp

thanks! :smiley: