Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sortable displayobjects #296

Closed
alaa-eddine opened this issue Aug 29, 2013 · 12 comments
Closed

Sortable displayobjects #296

alaa-eddine opened this issue Aug 29, 2013 · 12 comments
Labels
🤔 Question User question, similar to Help Wanted or Needs Help. These can be addressed whenever someone has tim

Comments

@alaa-eddine
Copy link

some times we need that display object to be rendered in a certain order to preserve the z-index.

it'd be good if we can have a way to choose the ordre of the drawn children, even with a sort method or by allowing a separate reder method for individual DisplayObjects so the developer can build it's ordered list, iterate throught objects and render them.

@englercj
Copy link
Member

The order of objects in the children array is the order they will be rendered. That means the last one in the array will be rendered "on top"

@alaa-eddine
Copy link
Author

now I feel a little dumb xD
when reading the source code I saw a linkedlist in renderDisplayObject and thought that stage.children array was gone in this release ....
nevermind, it's working just fine with stage.children sort :)

btw the 1.3.0 performances are just awesome ! I'm finishing a small code to test WebGL stuff on cocoonjs

thank you :)

@englercj
Copy link
Member

Not a problem, glad PIXI is working out for you!

@tengotengo
Copy link

var mapContainer = new PIXI.DisplayObjectContainer(),
    unitsContainer = new PIXI.DisplayObjectContainer(),
    menuContainer = new PIXI.DisplayObjectContainer();

mapContainer.zIndex = 5;
unitsContainer.zIndex = 10;
menuContainer.zIndex = 20;

/* adding children, no matter in which order */
stage.addChild(mapContainer);
stage.addChild(menuContainer);
stage.addChild(unitsContainer);

/* call this function whenever you added a new layer/container */
stage.updateLayersOrder = function () {
    stage.children.sort(function(a,b) {
        a.zIndex = a.zIndex || 0;
        b.zIndex = b.zIndex || 0;
        return b.zIndex - a.zIndex
    });
};

stage.updateLayersOrder();

@glebmachine
Copy link

Children array are readonly

@ivanpopelyshev
Copy link
Collaborator

@glebmachine doesnt mean that you cant call "sort"

@aquachris
Copy link

@tengotengo Thank you! Your solution is simple yet elegant.

@bluefeet
Copy link

bluefeet commented Nov 6, 2016

Is @tengotengo's solution really the final solution for this? Being able to change z-index seems to be a primary feature to me. Imagine, in an isometric game where depending on your y-axis one sprite could be considered behind or in front of another sprite. Doing z-index is the traditional way of handling this, and in that case you'd just map the sprites bottom y position to their z-index and done. For now I'll do the sort hack, but seems this should have an official solution.

@themoonrat
Copy link
Member

@bluefeet Take a look at https://github.com/pixijs/pixi-display
I think it'd give you the functionality you require

@loadedsith
Copy link

If you're here looking for this feature in 2017; it's been implemented in Pixi as zOrder.

https://pixijs.github.io/examples/#/display/zorder.js

@ivanpopelyshev
Copy link
Collaborator

@loadedsith pixi-display is updated to latest version, rewritten on TS and there'll be huge patch with a lot of examples this week.

@lock
Copy link

lock bot commented Feb 24, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Feb 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🤔 Question User question, similar to Help Wanted or Needs Help. These can be addressed whenever someone has tim
Projects
None yet
Development

No branches or pull requests

9 participants