Jump to content

Drawing order of sprites [solved]


Wolfsbane
 Share

Recommended Posts

Heya guys and girls,

This is just a quick fundamentals question on a basic: how do you determine the drawing order of sprites?(or any drawable instance).

It seems to be a stack (last sprite added to scene is drawn on top) which is actually working fine for my current purposes. But what if I wanted to draw a sprite always ontop? i.e. GUI, a hat on a character, water over a bridge, etc etc. Normally I expect to see some kind of layers, or depth value I can set, but I nothing obvious is popping out from the docs/source.

Cheers!

Link to comment
Share on other sites

Oh, okay, so I would actually manage it like that.

O.k. so take this scenario: A card game, spread so they're overlapping. I click one in the middle, and I want to pop it to the top of the pack. The best design practice in Panda would be to remove sprite from container, and pop it into another container? (or even just removing and re-adding to container, as the last one added it would be top again)?

Link to comment
Share on other sites

This feels a little hard to use? So this would be fine for the card game scenario, but how about for pseudo-3d games, such as a Isometric engines, or pseudo-3d style games (Outrun, Space Harriers, etc). Normally the order of sprites needs to be calculated and reordered on the fly for these.

 

 

Link to comment
Share on other sites

How it is hard to use?

Each container has children property, which is just a basic JavaScript array containing all objects (other containers, sprites, graphics etc) added to it. First object in the array will be renderer first, and so on. You can reorder that array in any way you want, even use your own sorting function.

For example here i am sorting all objects added to a container every frame, based on their y position, so that lowest y is on top:

game.createScene('Main', {
    init: function() {
        this.container = new game.Container();
        this.contaienr.addTo(this.stage);
        
        // Add objects to the container
    },
    
    sort: function(a, b) {
        return a.y > b.y ? 1 : -1;
    },
    
    update: function() {
        this.container.children.sort(this.sort);
    }
});

 

Link to comment
Share on other sites

Ah ha, yes, that's exactly like what I'm after. I took a peek at the container.js, and saw the children element, but I didn't quite click how I could use it.

 

Quote

How it is hard to use?

Well, it sounded like I'd have to be adding/re-adding sprites to change the draw order, which... sounds ugly.

Link to comment
Share on other sites

There is second parameter in addTo function, which you can use to define the index where the sprite will be added in the array:

sprite.addTo(container, 0); // Add to container at index 0 (which is first in the array)

You can also swap positions of two sprites in the array:

sprite1.swap(sprite2); // Swap positions of sprite1 and sprite2 in children array

So there are lots of ways to change the drawing order.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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