Closed danielmahon closed 10 years ago
Not sure about that, but have you tried:
javascript
this.sortBy = ig.Game.SORT.POS_Y;
At your games init function before loading a level?
ok, no more error (need to put after parent call...duh) but the entities still arent sorting...
Auto sort is disabled by default, I believe. Check the config file in core, and you can override it in your user config.
I have AUTO_SORT_LAYERS: true
set in config-user.js
as well
By the way you guys are awesome for responding for quickly :)
@collinhover Please feel free to correct me if i am totally wrong on this, but even if i change this line here:
https://github.com/collinhover/impactplusplus/blob/dev/lib/plusplus/core/layer.js#L207
manually to the POS_Y
sort method:
javascript
this.sortBy = function( a, b ){ return (a.pos.y+a.size.y) - (b.pos.y+b.size.y); };
I too get a:
`Uncaught TypeError: Cannot read property 'y' of undefined`
But to be honest, this is the first time that i took a look at the layer class, so i might misunderstand the whole concept ;-)
@danielmahon You're welcome! Feel free to contribute to his project if you like it. Collin and I are looking for suggestions for tutorials/examples at the moment:
https://github.com/collinhover/impactplusplus/issues/123
And if you would like to do an example on a specific topic later on, your help would be highly appreciated ;-)
Ah, I think the error is getting thrown by layers trying to sort background maps. Normally, when using a zIndex sort, the sort is just comparing direct properties of the sorted items. When it is switched to a x/y sort, the sort is comparing properties of the position property of the sorted items, and background maps have no position. This is a bug (thanks for finding it guys), and it should be a fairly quick fix.
Good to know that it's easy to fix! Removing the sortLayersBy
property seems to be a good idea to avoid confusion in the future ;-)
sounds good to me, thanks for looking into it
If I override the sortBy
method and disregard objects without pos
It still doesnt sort the entities, i havent dug deep enough yet to figure out how you are handling sorting different from core, point me in the right direction?
// this.sortBy = ig.Game.SORT.POS_Y;
this.sortBy = function (a, b) {
if (a.pos && b.pos) {
return (a.pos.y + a.size.y) - (b.pos.y + b.size.y);
}
};
Oops, my bad, sortLayersBy
is used for sorting the layers themselves, not the entities within the layers. sortBy
is indeed the correct way to sort entities.
@danielmahon can you add a console log at https://github.com/collinhover/impactplusplus/blob/master/lib/plusplus/core/layer.js#L672, and let us know if the sort is being called at all? I'll be able to look into this more tomorrow I think.
so yeah... I forgot to put this.sortEntitiesDeferred();
in my main update loop. It was sorting them on init, but not as they walked around. If you set sortBy
after the this.parent()
call in the main init it works fine, but if you set the sortBy
before then it fails and im not sure if that is intended behavior but I think it may be working as it should... oops
Usually, you should not need to call the deferred sort in your update loop, as this would be like resorting the entities every frame. However in your case this is correct as entities may move every frame.
I believe setting the sortBy after calling parent in init is correct, as the parent game will set the default sortBy in its own init.
Just one bug to fix :-)
@danielmahon you should not need to call this.sortEntitiesDeferred()
anymore with the recent fix/update. If you've set the layers to auto sort, they will resort when something in the layer has changed, which is usually more efficient than forcing a sort every frame.
awesome, thanks @collinhover and @Pattentrick for the help.
I have played with Impact.js a few times in the past, made some tiny games / interactive experiments but I may have the opportunity to actually devote some time to an upcoming project utilizing impact. If that happens I'll definitely try to contribute as much as possible.
You're welcome. Glad to hear about you plans. Good luck for your upcoming project ;-)
I cant seem to figure out how to sort by POS_Y properly in ++. Where do I define the sortBy method? Every way I have tried throws a "cannot read prop y of undefined" error.