pixijs / pixi-react

Write PIXI apps using React declarative style
https://pixijs.io/pixi-react/
MIT License
2.26k stars 173 forks source link

AnimatedSprite are not animating after texture chaging #359

Closed NestlyS closed 1 year ago

NestlyS commented 1 year ago

Description

Hello, I am writing a small game and have encountered some problem. It's about playing the animation after changing the texture props. I need to change the animation for the main character depending on which key is pressed right now. However, after another texture prop is sent to the AnimatedSprite component, everything stops.

I was looking for an answer and found it on reddit: Link. So all I need is just to call the play() function, but I don't have access to the AnimationSprite instance. The AnimationSprite component cannot work with ref prop and returns null instead of an instance.

Fortunately, I found a solution: I need to provide ref prop to Container component that wraps the AnimationSprite component, after that I get a children prop of an Container instance with an instance of AnimationSprite inside and call the playback function.

In my opinion that solution is pretty dirty, so I ask you to add auto call of play() function after textures prop changed or/and add ref prop support.

In my opinion, the solution is quite dirty, so I ask you to add an automatic call to the play() function after changing the textures prop or/and and add support for the ref props.

Steps to reproduce

It's quite easy and could be reproduced on demo codepen at that Link. I forked and made an example, where I've tried to bruteforce bug with my solution. You can see it here starting with 52 line.

So, steps are next

  1. Add an AnimationSprite component
  2. Try to change it's texture prop after first render.

Additional info

baseten commented 1 year ago

Adding ref support to AnimatedSprite seems like it would be a useful feature, I'll look into it 👍

baseten commented 1 year ago

Closing in favour of linked issues