# Follow Grid

**URL:** <https://forum.kirupa.com/t/follow-grid/296866>\
**Category:** flash\
**Created:** [September 22, 2009, 2:03am UTC](https://forum.kirupa.com/t/follow-grid/296866 "2009-09-22T02:03:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pi3rc3](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@pi3rc3](https://forum.kirupa.com/u/pi3rc3)\
**Post date:** [September 22, 2009, 2:03am UTC](https://forum.kirupa.com/t/follow-grid/296866/1 "2009-09-22T02:03:15Z")

</div>

i have a 4x4 block that i want to follow where the mouse is but constrained to a grid. each block in the grid is 32x32 pxs. heres code i have:

```auto
var distY = Math.sqrt((this.y-b.game.mouseLayer.mouseY)*(this.y-b.game.mouseLayer.mouseY));
			var distX = Math.sqrt((this.x-b.game.mouseLayer.mouseX)*(this.x-b.game.mouseLayer.mouseX));
			
			if(distX>34){
				if(this.x<b.game.mouseX){
					//b.fixed = false;
					b.x += 17;
					//b.fixed = true;
				}else{
					//b.fixed = false;
					b.x -= 17;
					//b.fixed = true;
				}
				
			}
			
			if(distY>34){
				if(this.y<b.game.mouseY){
					//b.fixed = false;
					b.y += 17;
					//b.fixed = true;
				}else{
					//b.fixed = false;
					b.y -= 17;
					//b.fixed = true;
				}
			}

```

but this doesnt work. i can sometimes get it to get onto the grid then if i move my mouse slowly to an outside distance it will then snap and constrain to grid like it should but shouldnt this code make it just work and follow mouse?
