Jump to content

How do you z-index sort a group and other sprites NOT in the group


fitness23
 Share

Recommended Posts

I have a group full of enemies that walk up and down throughout a scene which are all members of a group called "enemyGroup".

I also have a sprite called "player" which can't be a member of the enemyGroup.

Since the enemies and the player and running about the scene, how can I sort them based on their y value? If you need some context the game can be found here = http://www.andy-howard.com/side-scrolling-fighting-game/

I found a number of different people mentioning this in older forums posts but they were in relation to Phaser v1.2. My game is now on v2.4.8 and I was hoping things have moved on since then.

 

I already tried the following in the update function but it didn't work:

this.enemyGroup.forEachAlive(function (enemy) {
		    enemy.z = enemy.y;

		}, this);

		this.player.z = this.player.y;

		this.game.world.sort("z");

 

Link to comment
Share on other sites

If they are all in one Group, then you can't do this. Pixi uses a standard display list implementation (like in Flash), and a Group is a container. You can only sort within the Group itself. The only way to do it is to not have everything in Groups, and find another way to categorise them.

Link to comment
Share on other sites

Thanks for your reply @rich. What would be my approach if I removed groups? Would it be better in your opinion to put ALL characters in the same group and just add a new attribute to distingush between them. ie

character.type="player"
character.type="enemy"

 

Link to comment
Share on other sites

All Sprites in Phaser now have a data Object you can use. So you could do character.data.type = 'player', and won't need to add in extra properties.

Also I know that @ivan.popelyshev was working on a way to specify render order outside of containers. This was for Pixi 4, and I don't know how far he got, but it may be worth having a look.

Link to comment
Share on other sites

@rich

This was for Pixi 4, and I don't know how far he got, but it may be worth having a look.

That far. 

m9DDu6Rjvx6bP8fBFj95sSTa1sfUpO.png

@GoodBoyDigital said that's insane.

You look at https://github.com/pixijs/pixi-display , I believe it will be easy to port on phaser. "Tankwars.io" and some other projects are using it, everyone is satisfied with my API.

Link to comment
Share on other sites

Oh right, it does have module for handling input events. It handles them like before BUT things that are on top have priority over things that are at the bottom. Imagine that before you had a flag which you set to "true" when you found something. Now you have to deal with int - displayOrder of found element. If you find element with larger displayOrder, you will take it instead (with all that bubbling thing). 

I think you can port interaction too.

BTW, which algorithms for sorting do you use, @rich?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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