Jump to content

Possible to reorder sprites?


Recommended Posts

In State 0:

    I create a main sprite  and adjust its properties.


In State 1:

   I load in a tilemap, but the tile map displays over the sprite and some weird stuff happens to the main sprite, not displaying correctly.

   I tried to re-add it in this state via this.game.add.existing(mainSprite) but it doesn't work. 


Anyone have idea on how to solve this?


Here is some code for reference:

State0.jsthis.game.characters = {};      this.game.characters.lance = new Phaser.Sprite(this.game,10,100,'lance');//this.game.add.sprite(10, 100, 'lance');      //add animation from spritesheet      this.game.characters.lance.animations.add('walk-x',[40,41,42,43,44]);      this.game.characters.lance.animations.add('walk-up',[32,33,34,35,36]);      this.game.characters.lance.animations.add('walk-down',[16,17,18,19,20]);      this.game.characters.lance.animations.add('idle-up',[32]);      this.game.characters.lance.animations.add('idle-down',[16]);      this.game.characters.lance.animations.add('idle-left',[40]);      this.game.characters.lance.animations.add('idle-right',[40]);      //Add to the world      this.game.add.existing(this.game.characters.lance);State1.js   loadTilemap();   this.game.add.existing(this.game.characters.lance);
Link to comment
Share on other sites

Is that only for sprites under groups? It errors out : 

Uncaught Error: [object Object] The supplied DisplayObject must be a child of the caller [object Object]DisplayObjectContainer.js [348]	PIXI.DisplayObjectContainer.removeChildGroup.js [1125]	Phaser.Group.removeGroup.js [445]	Phaser.Group.bringToTop 
Link to comment
Share on other sites

if you're starting a new state without passing 'false' as the 'clearWorld' (2nd) paramter to StateManager.start(), then the world was cleared and you can't rely on having the sprites that you added in a previous state to still exist... if you don't clear the state, then the objects from the first state will continue to exist (all of them), which you may or may not want.


FYI, game.add.existing() adds the sprite to the world (which is a group), so that shouldn't matter.

(you also shouldn't need to bring the sprite to the top if you just added it, as add puts it at the top of the z-order)

Link to comment
Share on other sites

Wait so if I am in state1 and keep a reference to a sprite under this.game, and then move to the next state with clearWorld set to true, then is sprite under this.game still valid?  Valid meaning is the reference still good? If so should I be able to add it via this.game.add.existing or another way?


Maybe you can help me, I have a main character that the game revolves around, I want to set certain properties on the main character once at the beginning and have the ability to use that character in another state. What would be the proper way to implement this?

Link to comment
Share on other sites

I believe you are correct, it seems like something in the Sprite's internal state does not survive starting the new state with clearWorld set to true... I don't know if this is by-design or a bug. It seems likely that it is in the Pixi code, as Phaser calls Pixi to remove the objects, I believe. Rich should know, perhaps he'll chime in


as a work-around I would just keep another object that has the properties that you would like to keep on the main character sprite, and re-add those properties at the start of each state. (this is the way I'm currently doing things). tearing down and re-building that one sprite unnecessarily shouldn't be any real performance burden when an entirely new state is being entered anyway.

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...