dchhetri Posted December 7, 2013 Share Posted December 7, 2013 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 More sharing options...
jcs Posted December 7, 2013 Share Posted December 7, 2013 Sprite.bringToTop() will bring a particular sprite to the top of the z-order(internally TilemapLayers are just sprites, so they participate in the z-order just like any other sprite) Link to comment Share on other sites More sharing options...
dchhetri Posted December 7, 2013 Author Share Posted December 7, 2013 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 //state2:this.game.add.existing(this.game.characters.lance);this.game.characters.lance.bringToTop(); Link to comment Share on other sites More sharing options...
jcs Posted December 7, 2013 Share Posted December 7, 2013 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 More sharing options...
dchhetri Posted December 8, 2013 Author Share Posted December 8, 2013 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 More sharing options...
jcs Posted December 8, 2013 Share Posted December 8, 2013 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 More sharing options...
dchhetri Posted December 8, 2013 Author Share Posted December 8, 2013 I see, good to know I'm not going crazy lol, yea I'll just abstract the properties into a function and have it update the given sprite for now. I'll monitoring this thread for further input thanks man. Link to comment Share on other sites More sharing options...
Recommended Posts