GiniWren Posted June 2, 2018 Share Posted June 2, 2018 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 More sharing options...
GiniWren Posted June 5, 2018 Author Share Posted June 5, 2018 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 More sharing options...
samme Posted June 6, 2018 Share Posted June 6, 2018 Yes, this seems to be a bug, since it doesn't check that parent.bringToTop is the right kind. You can use setChildIndex instead. GiniWren 1 Link to comment Share on other sites More sharing options...
GiniWren Posted June 7, 2018 Author Share Posted June 7, 2018 Thanks @samme -- filed a bug with Phaser CE just so it's documented, but setChildIndex works perfectly for what I need. (Also from now on gonna use that codepen setup from the CE bugs page to make code examples easier to check out ☺️) Link to comment Share on other sites More sharing options...
Recommended Posts