Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to change order of event listeners of a displayObject
Started by carloshritzmann Mar 02 2019 02:09 PM

3 replies to this topic
displayobject event listener reorder listeners

Best Answer yvandotet , 03 March 2019 - 09:32 AM

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.

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

carloshritzmann

[GLOBAL: userInfoPane.html]
carloshritzmann
  • Observer

  • 14 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

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.



[TOPIC: post.html]
#3

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 185 posts
  • Corona SDK

  Best Answer

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.



[TOPIC: post.html]
#4

carloshritzmann

[GLOBAL: userInfoPane.html]
carloshritzmann
  • Observer

  • 14 posts
  • Corona SDK

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! :D




[topic_controls]
[/topic_controls]