Jump to content

event emiter/listener


renathy
 Share

Recommended Posts

What is the best way to emit events from one sprite and listen them on another sprite or container?

 

1) I have toolbar with buttons like "delete, resize etc.' . When user clicks on sprites on the screen (a lot of them), I need to emit "Sprite is clicked" and save "Sprite" into event's data.

So, toolbar will listen to the event and will know which sprite to be "delete, resize etc.".

 

2) Second scenario is when user clicks on custom sprite (Background: PIXI.Sprite), I need to change background of another Sprite. I have multiple Background sprites.

In this case "Background" click should Emit event. And Sprite will listen and change its background.

Link to comment
Share on other sites

you can use Observe 
https://www.npmjs.com/package/observe/v/1.2.0
or this one is better, i using it with React, because everything is immutable (@read-only).
https://www.npmjs.com/package/@nx-js/observer-util

 

Or in vanilla, but why reinvent the wheel?
https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent

 

Actualy PIXI natively give you event for listen child's change, but not for props, or is missing in the doc. ! 
A PIXI guys can maybe fix me if am wrong.

 

Note: 
Do not confused with the obsolete native version in Js vanilla, it was removed
https://developer.mozilla.org/en-US/docs/Archive/Web/JavaScript/Object.observe
"You can use the more general Proxy object instead." is what @nx-js/observer-util do
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy

Edited by jonforum
Link to comment
Share on other sites

  • 2 months later...

EventEmitter  can not help on this?

PS: I am just trying to implement something like topic starter was. So, I am searching for understandable solution for me. I am not expert in pixi... and in javascript too.

I would be nice to show me a small example, how I can do this ;)

Edited by flatliner
add small postscriptum
Link to comment
Share on other sites

Eventually, I need only one solution (if it possible). Using EventEmitter can we use some one object (like stage or canvas) to emit our event to it, and it will propagate this event to all its child objects? Or we need keep array of all objects to emit events for each of them one by one?

Edited by flatliner
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...