collinhover / impactplusplus

Impact++ is a collection of additions to ImpactJS with full featured physics, dynamic lighting, UI, abilities, and more.
http://collinhover.github.com/impactplusplus
MIT License
276 stars 59 forks source link

How to sortBy POS_Y? #131

Closed danielmahon closed 10 years ago

danielmahon commented 10 years ago

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.

Pattentrick commented 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?
danielmahon commented 10 years ago

ok, no more error (need to put after parent call...duh) but the entities still arent sorting...

danielmahon commented 10 years ago

colorsnapper_and_impact_game

collinhover commented 10 years ago

Auto sort is disabled by default, I believe. Check the config file in core, and you can override it in your user config.

danielmahon commented 10 years ago

I have AUTO_SORT_LAYERS: true set in config-user.js as well

danielmahon commented 10 years ago

By the way you guys are awesome for responding for quickly :)

Pattentrick commented 10 years ago

@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 ;-)
collinhover commented 10 years ago

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.

Pattentrick commented 10 years ago

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 ;-)

danielmahon commented 10 years ago

sounds good to me, thanks for looking into it

danielmahon commented 10 years ago

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);
                }
            };
collinhover commented 10 years ago

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.

danielmahon commented 10 years ago

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

collinhover commented 10 years ago

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 :-)

collinhover commented 10 years ago

@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.

danielmahon commented 10 years ago

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.

Pattentrick commented 10 years ago

You're welcome. Glad to hear about you plans. Good luck for your upcoming project ;-)