Jump to content

[SOLVED] Sprite's bringToTop not bringing child sprite to top of children


GiniWren
 Share

Recommended Posts

Hey there,

Having a bit of trouble today with the bringToTop function of the Phaser sprite. I have a sprite, to which I've added two child sprites. However, when I call bringToTop on one of the child sprites, nothing appears to happen.

Anyone know why this might be happening? I'm not sure if there's something I'm missing. Thanks for any help!

----------

Using Phaser CE v2.10.5

Seen in browsers:

  • Google Chrome, Version 66.0.3359.170 (Official Build) (64-bit)
  • Firefox Quantum, Version 60.0 (64-bit)
  • Safari, Version 11.1 (13605.1.33.1.2)

Minimum reproducible code:

var game = new Phaser.Game(256, 223, Phaser.AUTO, 'game-container',
  { preload: preload, create: create });

function preload() {
  // load game images
  game.load.image('container', 'img/container.png');
  game.load.image('star', 'img/star.png');
  game.load.image('blueStar', 'img/blue-star.png');
}

function create() {
  // create container sprite
  var container = game.add.sprite(0, 0, 'container');

  // create two additional sprites
  var yellowStar = game.add.sprite(40, 40, 'star');
  var blueStar = game.add.sprite(40, 40, 'blueStar');

  // make both sprites children of the container sprite
  container.addChild(yellowStar);
  container.addChild(blueStar);

  // in 2 seconds, bring the first/bottom sprite to the top
  var timer = game.time.create();
  timer.add(2000, function() {
    yellowStar.bringToTop();
  });
  timer.start();
}

 

Link to comment
Share on other sites

Able to work around this by calling the sprite's removeChild and addChild functions -- certainly not the worst work-around, as that's how Phaser Group handles this functionality anyway. Sprite's bringToTop appears to not work because it calls bringToTop on each non-group object's parent, presumably until it reaches the world level (the docs seem to confirm this), but it looks like it leaves the ordering of the sprite's children alone.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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