Yeah, I’ll help you with programming whenever I have answers. For your puzzle mania game, to get the snapping to work, I think I would first check the piece’s x and y position, then compare it to all the snap points, and see which it is closest too. Then, I would set its position at the snap point. It should look something like this:
//…
show moreYeah, I’ll help you with programming whenever I have answers. For your puzzle mania game, to get the snapping to work, I think I would first check the piece’s x and y position, then compare it to all the snap points, and see which it is closest too. Then, I would set its position at the snap point. It should look something like this:
//Check your first snap point, in the top left corner using the distance formula
var distance1 = Math.sqrt(Math.pow(this.x – 0, 2) + Math.pow(this.y – 0, 2);
//Then check the distance from the next snap point (let’s call it (3,0))
var distance2 = Math.sqrt(Math.pow(this.x – 3, 2) + Math.pow(this.y – 0, 2);
//Calculate distance from all snap points
distance3 =
distance4 =
distance5 =
…
//Then, compare the distances to find the closest one (assuming the distances have been put in an array called “distances”)
var closest = 1000;
var closestPoint = 0;
for(var a in distances)
{
if (closest > distances[a])
{
closest = distance[a];
closestPoint = a;
}
}
//Then set the position to the closest snap point (assuming you have an array of all the x and y values of the snap points, named “x” and “y”)
this.x = x[closestPoint]
this.y = y[closestPoint]
And that should make the pieces snap into place!
show less