Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Krasik

Hello.
I&039;m having some issues with dynamic sorting using #ig.game.sortEntitiesDeferred()
Just need to sort entities by zIndex and it works but something weird is happening on my screen.
This is what im doing:

In entity update()
this.zIndex = Math.floor(this.pos.y);

in main update()
if (typeof ig.game.layersMap["entities"] != "undefined") {

    for (var i = 0; i < ig.game.layersMap["entities"].items.length - 1; i++) {

        if (ig.game.layersMap["entities"].items[i].zIndex >
            ig.game.layersMap["entities"].items[i + 1].zIndex) 
            ig.game.layersMap["entities"].items.sort(function(a,b)
            { return a.zIndex - b.zIndex; });
    }
}

It&039;s exactly what #ig.game.sortEntitiesDeferred() do but im doing it only when zIndex changes.

This is what happening on my screen with main update code:
https://www.youtube.com/watch?v=mpwy6KdQnnU&feature=youtu.be
Followed entities are lagging a bit.

And this is without sorting code:
https://www.youtube.com/watch?v=23AihUHltLk&feature=youtu.be
Every move is smooth.

This lagging problem also occurs when autoSort in ig.game is set on true. This not helping with sort by zIndex, only lagging entities.
Any thoughts on how to fix?

1 decade ago by Joncom

There is a simpler way to do what you want.

1) Remove the following from your main update function:
if (typeof ig.game.layersMap["entities"] != "undefined") {

    for (var i = 0; i < ig.game.layersMap["entities"].items.length - 1; i++) {

        if (ig.game.layersMap["entities"].items[i].zIndex >
            ig.game.layersMap["entities"].items[i + 1].zIndex) 
            ig.game.layersMap["entities"].items.sort(function(a,b)
            { return a.zIndex - b.zIndex; });
    }
}

2) Then remove the following from your entity update function:
this.zIndex = Math.floor(this.pos.y);

3) Make sure ig.game.autoSort equals true.
4) Set ig.game.sortBy equal to ig.Game.SORT.POS_Y.
Page 1 of 1
« first « previous next › last »