moszis Posted June 30, 2014 Share Posted June 30, 2014 Hi Guys, Is there a way to specify that sprite should go on top (similar to the way its done in phaser)? I need to have multiple draggable sprites. Currently the last one drawn is always on top. So when you are dragging sprites, they will be dragged under the sprites that have been drawn later. I know it can be easily done by just clearing and redrawing the sprite every tick but it would make it much easier if there was an API to bring it to top layer on mousedown. PS: I know its two dimensional pixel grid(this seems to be common answer to these type of questions).. just talking about APIs here. Quote Link to comment Share on other sites More sharing options...
bubamara Posted June 30, 2014 Share Posted June 30, 2014 PIXI.Sprite.prototype.bringToFront = function() { if (this.parent) { var parent = this.parent; parent.removeChild(this); parent.addChild(this); }}and then :mySprite.mousedown = mySprite.touchstart = function() { this.bringToFront();} moszis and dirkk0 2 Quote Link to comment Share on other sites More sharing options...
moszis Posted June 30, 2014 Author Share Posted June 30, 2014 Perfect! Thank you very much! This also gives me enough understanding of pixie hierarchy to solve several other issues. Very much appreciated! Quote Link to comment Share on other sites More sharing options...
mrBRC Posted July 2, 2014 Share Posted July 2, 2014 (edited) An alternative form.. without prototypingfunction bringToFront(sprite, parent) {var sprite = (typeof(sprite) != "undefined") ? sprite.target || sprite : this;var parent = parent || sprite.parent || {"children": false};if (parent.children) { for (var keyIndex in sprite.parent.children) { if (sprite.parent.children[keyIndex] === sprite) { sprite.parent.children.splice(keyIndex, 1); break; } } parent.children.push(sprite);}}function sendToBack(sprite, parent) {var sprite = (typeof(sprite) != "undefined") ? sprite.target || sprite : this;var parent = parent || sprite.parent || {"children": false};if (parent.children) { for (var keyIndex in sprite.parent.children) { if (sprite.parent.children[keyIndex] === sprite) { sprite.parent.children.splice(keyIndex, 1); break; } } parent.children.splice(0,0,sprite); }}uses.. mySprite.mousedown = bringToFrontbringToFront(mySprite);bringToFront(mySprite, newDisplayObjectContainer); //pops it out of its original container/parentand of course..mySprite.bringToFront = bringToFront;mySprite.bringToFront();mySprite.bringToFront(null, newDisplayObjectContainer);EDIT: opps... I was using pop incorrectly.. it doesn't take a parameter, so I am using a cross browser legacy compatible implementation. Reference: http://stackoverflow.com/questions/5767325/remove-specific-element-from-an-array/15762329#15762329 Edited July 16, 2014 by mrBRC moszis 1 Quote Link to comment Share on other sites More sharing options...
staff0rd Posted June 13, 2016 Share Posted June 13, 2016 addChild (now at least) checks for a parent and removes it, so you can just call this; sprite.parent.addChild(sprite); Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted June 13, 2016 Share Posted June 13, 2016 @staff0rd https://github.com/pixijs/pixi-display Quote Link to comment Share on other sites More sharing options...
staff0rd Posted June 14, 2016 Share Posted June 14, 2016 @ivan.popelyshev should I be using v4 even though its pre-release? Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted June 14, 2016 Share Posted June 14, 2016 @staff0rd We are almost there: https://github.com/pixijs/pixi.js/issues/2642 Quote Link to comment Share on other sites More sharing options...
imShyness Posted August 5, 2016 Share Posted August 5, 2016 This prototyping methode, can it be also used for containers? I have about 4 draggable containers and the selected one always needs to be on top. Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted August 5, 2016 Share Posted August 5, 2016 @imShyness I don't see why not Quote Link to comment Share on other sites More sharing options...
labrat.mobi Posted August 7, 2016 Share Posted August 7, 2016 on mouse down just add the sprite to stage again, that should bring the sprite froward.. shouldn't that fix it? Quote Link to comment Share on other sites More sharing options...
Karan Batta Posted March 2, 2020 Share Posted March 2, 2020 Is there any latest efficient way to do this?? i.e. making a graphic object child of its grandparent i.e. removing parent in the ^^linked list^^. Quote Link to comment Share on other sites More sharing options...
Dougi Posted July 29, 2021 Share Posted July 29, 2021 (edited) I've just stored the zIndex on each of the display objects and then when you want to move the display objects to the top you just find the highest zIndex, add 1 and set the zDepth on the display objects to that. Works well and pretty straight forward. You need to put the display objects in a container and set that as sortable too. Edited July 29, 2021 by Dougi Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.