I know this is a somewhat old thread but I'll post this for you anyways.
You don't have to check if the X axis is "roughly" the same, you can calulate if one crate is above another if you have a width and a height assigned to the crate entities.
The way you should go about this is firstly have a property on the crates called "canPickup" or something, this will be a boolean obviously. Now in the update() function you want to loop through all other crates (get an array of all of them by using getEntitiesByType()).
Pro-tip: Looping through an array backwards is faster than looping forwards in Javascript. This is done like so "for(var i = crates.length;i--;)".
Now for every crate you have to do alittle bit of math to figure out if it's "on top" (Don't worry, it's not bad at all). We will do this one axis at a time and we will start with the X axis.
Simply take the X axis of your current crate (the one where the update() function is currently executing) and the X axis of the "other" crate (the one selected in the loop) and subtract them. Make sure the number you get is not negative and then compare it to the
width of the crates. If the number is smaller than the
width it means the crate is "on top" on the X axis. The formula / code for this looks like this:
var diffX = Math.abs(this.pos.x - other.pos.x);
if(diffX < this.width){
//On top on the X axis.
}
The
Math.abs() function makes sure the number is not negative.
Now lets handle the Y axis, this one is even easier.
We simply check if the Y axis + the
height of the crate of the "other" crate is smaller than the Y axis of "this" crate. Like so:
if((other.pos.y + other.height) < this.pos.y){
//On top on the Y axis.
}
That's it. Now based on this we will set our "canPickup" property to either true or false. I've included a full example of the update() function below.
var otherCrates = ig.game.getEntitiesByType(EntCrate);
for(var i = otherCrates.length;i--;){
//Skip this iteration if the crate from the array is "this" crate.
if(otherCrates[i] === this)
continue;
var diffX = Math.abs(this.pos.x - otherCrates[i].pos.x);
//Notice the exclamation mark here, it means if the two statements are true, false will be returned and if the two statements are false, true will be returned.
this.canPickup = !(diffX < this.width && (otherCrates[i].pos.y + otherCrates[i].height) < this.pos.y);
//Terminate the loop if we have already determined that a crate is on top.
if(this.canPickup)
break;
}
I hope this helps you out in case you didn't fix the problem already.