# snap sprite to grid in game

0 favourites
• 7 posts
From the Asset Store
Snap to visible grid - perfect solution for any game genre
• Hey guys,

I created this little game with falling blossoms that make sounds, when you drag them into a tree they stick, and when a "time arrow" passes over them they play their sound. My problem is I need them to snap the blossoms to some sort of grid when the drag and drop is released, or else its impossible to make a little tune with any structure.

So basically, how to I get sprites to snap to a grid when they're dropped by the cursor?

Dougx

• I chucked it up online so you guys can see what I'm on about!

:)

• In a nutshell:

1. Determine the size of your grid tiles such as 16x16

2. Take the x coordinate of your sprite such as 77.

3. Divide that coordinate by the size of your grid tile so 77/16 = 4.8125

4. Discard the .8125 by using the floor(77/16) = 4

5. Multiply your new value by the size of your grid tile so 4 x 16 = 64.

What this does it removes the fractional component of each coordinate. So any x coordinate from 0 to 15 will snap to 0. From 16 to 31 will snap to 16. From 32 to 47 will snap to 32. From 48 to 64 will snap to 48.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Thanks man, I really appreciate the help, but I'm a little confused. How do I go about implementing this? (be gentle I picked up construct yesterday)

Also, just to check, when all this is done, will I be able to drop a sprite anywhere and it will snap to the nearest point to it on the grid?

sorry to be a pain, you've been really helpful :)

• A simpler way:

• On Drag and Drop Dropped
• > Object set X to Object.X - (Object.X % CellSize)
• > Object set Y to Object.Y - (Object.Y % CellSize)
• Both really helpful guys :) thanks a bunch!

• 7 posts