I am programming a game in HTML5 canvas. If a line touches any point of its own or opponents line, that line stops moving. The Function I wrote follows below.
function hasPoints(x, y) {
for (var i = 0; i < points.length; i++) {
if (Math.abs(points[i][0] - x) < 2 && Math.abs(points[i][1] - y) < 2) {
return true;
break;
}
}
return false;
}
//check if this point crosses other players' points
if (hasPoints(p1.x,p1.y) || canPlay==false) {
clearLoop();
}else{
context.fillStyle = "green";
context.fillRect(p1.x,p1.y, 5,5);
addPoints(p1.x,p1.y);
sendMyPoints();
loopTimer = setTimeout('drawLine()', 50);
}
It works most of the time. But in some special case(As you can see in the picture below) it simply fails to return false.
Could anyone help me to improve this function, so that it works flawless?