I have two functions, one that calculates the mouse distance from an object and the other one calculates the player score. It starts scoring when the mouse is at least 200px away from the object and the closer the mouse is the more score you get.
function calculateMouseDistance() {
document.onmousemove = handleMouseMove;
var rect = c.getBoundingClientRect();
function handleMouseMove(e) {
mouseX = e.clientX - rect.left;
mouseY = e.clientY - rect.top;
}
mouseDistanceX = object.x - mouseX;
mouseDistanceY = object.y - mouseY;
mouseDistance = Math.round(Math.sqrt((mouseDistanceX*mouseDistanceX)+(mouseDistanceY*mouseDistanceY)));
}
function calculateScore() {
if (mouseDistance<200) {
score = Math.round(score + (mouseDistance / 100));
}
document.getElementById("tester").innerHTML = "Score: " + score + " " + "Distance: " + mouseDistance;
}
For some reason the above results in the score stopping when distance is 50px and below.
I tried outputting the (mouseDistance<200) condition and it was just fine, at 200px distance it said "true" and it only stopped being true above 200px.
If so then the score shouldn't stop updating when mouse is at 50px distance, but it does :P halp
(Also, if you can think of a better title tell me please so I can edit it. I don't think mine is that fitting although I can't think of anything better. For future reference)
EDIT: I output this Math.round(mouseDistance/100) and at 50px this line of code gives 0... but I don't understand why, probably from the line above score = Math.round(score + (mouseDistance / 100)); but still I don't get it
Aucun commentaire:
Enregistrer un commentaire