cloakedninjas Posted July 24, 2015 Share Posted July 24, 2015 I've not had my morning coffee so maybe I'm being slightly more dense than usual .... Here's some pseudo code of something I'm working onclass ParentSprite extends Phaser.Sprite { childSprite:Phaser.Sprite; constructor(game, x, y) { super(game, x, y, 'sprite-name', 'frame-name'); this.childSprite = new Phaser.Sprite(game, 0, 0); this.addChild(childSprite); game.add.existing(this); }}class ChildSprite extends Phaser.Sprite { constructor(game, x, y) { super(game, x, y, 'sprite-name', 'frame-name-2'); }}I'm having trouble getting the child sprite to be updated and positioned relative to its parent. http://www.html5gamedevs.com/topic/2813-attach-sprite-to-sprite/ has helped me somewhat but I'm still a little lost... What does addChild() actually give me? Do I still need to add my child sprite into the game with game.add.existing(this) ? If I move my parent sprite, will my child move too? /goes to get morning coffee Link to comment Share on other sites More sharing options...
Ryuuke Posted July 24, 2015 Share Posted July 24, 2015 Hi, You don't need to call game.add.existing(this) for your child sprite, the position of the child is always relative to the parent sprite position and when the parent sprite moves, the child moves too automatically. Link to comment Share on other sites More sharing options...
Tom Atom Posted July 24, 2015 Share Posted July 24, 2015 Hi, this is minimal example: var sprite = this.add.sprite(200, 200, "Atlas", "Frame1"); var sprite2 = new Phaser.Sprite(this.game, 50, 50, "Atlas", "Frame2"); sprite.addChild(sprite2); this.add.tween(sprite).to({ x: 400 }, 2000, Phaser.Easing.Linear.None, true, 0, -1, true);- first sprite is added through this.add and it is automatically added to stage, thus visible on screen. Second sprite is not added to any group (so, it is not on screen).- in third line sprite2 is added as child of sprite - so sprite 2 will be positioned relative to sprite (50, 50) and becames visible on screen,- last line makes both of them move. I am movin only parent sprite, but as it is root of this small hierarchy, sprite2 moves along too. Link to comment Share on other sites More sharing options...
cloakedninjas Posted July 24, 2015 Author Share Posted July 24, 2015 Well for me, if I don't add call game.add.existing(this) or for that matter game.add.existing(childSprite) I never see my sprites rendered out, nor do their update() functions get called. Umm... Link to comment Share on other sites More sharing options...
cloakedninjas Posted July 24, 2015 Author Share Posted July 24, 2015 Ok thanks for pointing me in the right direction - I think I have a broken entity in the hierarchy causing my lowest children to not be rendered. Link to comment Share on other sites More sharing options...
glennsjostrom Posted November 11, 2015 Share Posted November 11, 2015 Ok thanks for pointing me in the right direction - I think I have a broken entity in the hierarchy causing my lowest children to not be rendered. Same here. Did you solve it? Why will not this work?this.player = new PlayerSprite(this.game, 50, 50, 'atlas', 'player.png');this.gun = new Phaser.Sprite(this.game, 50, 50, 'atlas', 'gun.png');this.player.addChild(this.gun);this.game.add.existing(this.player); Link to comment Share on other sites More sharing options...
Recommended Posts