Sign in to follow this  
renathy

event emiter/listener

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.

Share this post


Link to post
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

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.