Sign in to follow this  
ChubbRck

Child sprite does not respect group/depth/layering of parent?

Recommended Posts

Hi there,

 

I'm having an issue where a child sprite does not respect the depth of the parent's group. Meaning that other objects will render on top of the parent sprite as expected, but not the child sprite, which appears on top of both the parent (expected) AND other objects (unexpected).

 

First, I am extending the sprite class:

var Wall = function (game, x, y, width, height) {    Phaser.Sprite.call(this, game, x, y, 'wall-border');    this.width = width;    this.height = height;    this.anchor.setTo(0, 1);    game.physics.arcade.enable(this);    this.body.immovable = true;     var child = this.addChild(game.add.tileSprite(0,0,100,100,'wall-pattern'));    // this.game.add.existing(child); // (THIS LINE IS THE PROBLEM)    child.x = this.x + 10;    child.y = this.y - 10;    child.anchor.setTo(0, 1);    child.width = this.width - 20;    child.height = this.height - 20;}

The problem is that the tileSprite created as a child does not appear in game unless the commented out line is added, but if so, it appears on top of all other objects (does not respect the parent's group ordering). Do I need to add the child to the same group as the parent or....? I know I'm doing something dumb, so any guidance is appreciated.

 

Thank you!

 

Share this post


Link to post
Share on other sites

You've got a couple of things going on:

 

1. The commented out line is extraneous; you don't need it. By calling "game.add.tilesprite" you're creating a tilesprite and adding it to the world. By passing the result to "this.addChild" you are removing it from the world and adding it as a child of the current sprite. If you then called "game.add.existing" that would *remove* it from the current sprite and add it to the world in front of everything... not what you want.

 

2. Children are positioned relative to their parent. When you say "child.x = this.x + 10" the child will be positioned AWAY from its parent, possibly by a large amount. You probably mean something like "child.x = 10". Similarly for y.

Share this post


Link to post
Share on other sites

A follow-up question for anyone. My child sprites are being rendered beneath their parent whereas they should be drawn on top. I can't imagine why this would be.  Anyone have any ideas? 

EDIT: My bad, I was adding the child sprites to another group

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.