$player
is at the center of the screen and can be moved at 360 following the mouse angle direction continuously, when I move it I also change the position of $map
in the opposite direction, maintaining the player at center and creating a "moving" effect in the map...
This is fired on mousedown:
var top1 = $map.offset().top;
var left1 = $map.offset().left;
var top2 = ($player.offset().top - $player.height()/2) - ($map.offset().top);
var left2 = ($player.offset().left - $player.width()/2) - ($map.offset().left);
if(interval1 == 'false'){
interval1 = setInterval(function(){
var speed = 1;
var deltaX = Math.cos(angle * Math.PI / 180) * speed;
var deltaY = Math.sin(angle * Math.PI / 180) * speed;
$map[0].style.left = (left1 -= deltaX)+'px';
$map[0].style.top = (top1 -= deltaY)+'px';
$player[0].style.left = (left2 += deltaX)+'px';
$player[0].style.top = (top2 += deltaY)+'px';
}, 1);
}
I do this on mouseup:
clearInterval(interval1);
interval1 = 'false';
The problem is that every time I fire the mousedown it adds some little gaps to the player so at every click it moves a bit from the center...
How can I fix this problem? I was not able to figure out what generates the gap...