Phaser layers

Recommended Posts

You can't create a single tree sprite if you want some parts of it to occlude and others to be occluded, you have to split the sprite into the parts which are above the player and the parts which aren't, and render the parts which are above the player on another layer which is always rendered on top of the player.

Share this post

Link to post
Share on other sites

@lewster32 I tried it, I use the trunk and leaves, how can I attach the trunk into the leaves, btw both of them are randomly spawned. I want both are them are in the same position.


function addOneTree_1(x, y) {
    var leaf = game.add.sprite(x, y, 'tree_1_leaves');
    var trunk = game.add.sprite(x, y, 'tree_1_trunk');
    leaf.body.setSize(10, 50, 77, 215);
    leaf.body.immovable = true;

    trunk.body.setSize(10, 50, 77, 215);
    trunk.body.immovable = true;

function addRowOfTree_1() {
    var hole = Math.floor(Math.random() * 5) + 1;

    for (var i = 0; i < 8; i++)
        if (i != hole && i != hole + 1)
          this.addOneTree_1(i * 440 + 10 * Math.floor(Math.random() * 500), i * 440 + 10 * Math.floor(Math.random() * 100));

function treesExecute() {
  leaves =;
  trunks =;
	for (var i = 0; i < 100; i++) {


Share this post

Link to post
Share on other sites

You can simply apply the same randomly taken location to both the trunk and leaves (only picking a new random for a each new tree). Then by adjusting the position of the leaves relative to the trunk or by setting an anchor you can make the leaves attach to the tree correctly.

Share this post

Link to post
Share on other sites

I use groups to manage what's in front of what. They're the only things added directly to the world; everything else is added to one of these groups. I have a lot: foreground, player, background, farBackground... Anything added to foreground is in front of the player group, for instance. That way I'm not always calling sendToBack or whatever to try and order my sprites.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.