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 Gommeth

My idea is to use a piece of code like...

var i = 0;
var ents = ig.game.entities

	while (i < ents.length) {
		ig.game.entities[i].zIndex = ig.system.height - ig.game.entities[i].y;
		++i;
	}
ig.game.sortEntities();

The desired effect would be that the draw order goes from top to bottom, so that the occlusion looks.. correct. Currently my problem is that when I use an alert, the zIndex appears for the first loop before map load as all 0's and the y's are undefined, and then after that both the zIndecies and the y's are both undefined.

Also, I've only used this engine and language for three days. Will this be too resource-heavy of an operation? Will it really decrease game performance if done maybe 4-5 times/sec?

Thanks in advance for all the help :D

1 decade ago by dominic

ig.game.entities[i].y is probably undefined. Doing math with an undefined value will always result in an undefined value - so after your first loop, all the zIndex are undefined as well.

I think you were looking for ig.game.entities[i].pos.y :)

But let&039;s step back a bit and have a look at the #sortEntities() method in impact/game.js:
sortEntities: function() {
	this.entities.sort( function(a,b){ return a.zIndex - b.zIndex; } );
}

It just uses the built-in sort() method of an array with a callback function to sort by zIndex. So why not just write another function that sorts by y position and get rid of the loop to set the zIndex completely. E.g.:
sortEntitiesByYPosition: function() {
	this.entities.sort( function(a,b){ return a.pos.y - b.pos.y; } );
}

How fast the sorting is, depends on the number of entities you have in your game and how "sorted" they already are. I haven't tested it, but my guess is that you can easily sort a few thousand entities in each frame on a decent desktop computer. I.e.: don't worry too much :)

1 decade ago by Gommeth

Thank you dominic for your quick, helpful responses (and your brilliance) :D
Page 1 of 1
« first « previous next › last »