I have an array that is part of a small JS game I'm working on. I need to check (as is often reasonable) that each of the elements in the array has not left the “scene” or “pad”, so I can delete them and save the script loading
I coded below and wondered if anyone knows a faster / more efficient way to calculate this. This is done every 50 ms (we are talking about movement).
Where bots[i][1]is the motion in X, and bots[i][2]is the motion in Y (mutually exclusive).
for (var i in bots) {
var left = parseInt($("#" + i).css("left"));
var top = parseInt($("#" + i).css("top"));
var nextleft = left + bots[i][1];
var nexttop = top + bots[i][2];
if(bots[i][1]>0&&nextleft>=PLAYGROUND_WIDTH) { remove_bot(i); }
else if(bots[i][1]<0&&nextleft<=-GRID_SIZE) { remove_bot(i); }
else if(bots[i][2]>0&&nexttop>=PLAYGROUND_HEIGHT) { remove_bot(i); }
else if(bots[i][2]<0&&nexttop<=-GRID_SIZE) { remove_bot(i); }
else {
$("#" + i).css("left", ""+(nextleft)+"px");
$("#" + i).css("top", ""+(nexttop)+"px");
}
}
Similarly, note remove_bot (i); the function is as shown below, is it correct (I can not merge, since it changes all the IDs of the elements in the array.
function remove_bot(i) {
$("#" + i).remove();
bots[i] = false;
}
Thanks so much for any advice!