Jump to content

Transfering sprites between states


Recommended Posts

Haven't been able to find anything in search-- apologies if this has been covered before.


In my game, I have a "players" object that's stored on the Phaser game object for convenience (i need these properties across several states). The players object holds data about each player in the game, one of those properties being a reference to a sprite (part of the game involves customizing your own character sprite, including attaching child sprites)


It works great, except that I'm having trouble transfering sprites that have already been added to the game world in one state, to another state. I am aware that I can keep sprites around by not clearning the world on state transition, but since there are many sprites I want to clear and only a few I want to keep, this seems inefficient.


Here's a codepen showing the problem. Check out the console: Phaser throws "TypeError: this._bindings is undefined" when the this.game.add.existing line executes.




What am I missing?






Link to comment
Share on other sites

Because ending a state destroys the whole display list recursively, you can save any sprites from this process by removing them from the display list in the shutdown function of the ending state: http://codepen.io/lewster32/pen/XJWJde


Alternatively, you could extend the standard Sprite object to have different functionality for its destroy method so that by default those objects are not blitzed.

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.

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.


  • Recently Browsing   0 members

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