A snapping puzzle to the grid


I know there are few old topics on the forum about grid, but they are old and have old project examples format (gdg). I’ve tried few things from those topics, but I can’t handle…

I just want to make most simple puzzle game. For now I have pieces on one part of the screen and board for which I can put on the puzzles. The rest of screen has collider which makes the pieces back to their original position when I drop them there. It’s fine, but I the thing is, my puzzles has no grid and I can put them on board chaotic, while I need them to be snapped in order :slight_smile:
Any ideas how to do this in some simple way? :slight_smile:

I use for testing assets from “pieces” example :slight_smile:

You could do your grid as pieces put together instead of a background image
each grid piece a Sprite then when you release the puzzle piece check if it is over a grid piece and if it is center it.

1 Like

yes, I thought about something like that :slight_smile: But could you tell me which events and actions use to achieve that effect? I’m not good in variables and numbers :sweat_smile:

i guess your objects have draggable behavior. You have is dragging event, I don’t have a computer at the moment bit look for this event, then you need a variable to know which object is being dragged. When you release mouse get X and Y coordinates of mouse.
Is a bit trickky, i started the code but left incomplete before finishing.

1 Like

Thanks, I’ll try to do something but I’m not sure if I can handle :slight_smile:

will rescue my unfinished Code i think it will help

1 Like

I’m looking for some help with this too.

I can get the snapping working by placing sprite objects on my canvas then the draggable objects have conditions like "when not being dragged & collide with ‘placeholder object’ " then “put an object around another”.

This makes them snap perfectly, however, I also wanted to make it so two pieces could not snap at the same time but I’m having trouble making it do that.

thanks :slight_smile: this time I figured something else for my game purposes, but I’m sure your code will appreciated not only by me :slight_smile:

No problem. I decided to move away from full snapping and only allow snapping to the correct placeholder, otherwise I use tween to make the object bounce back to the original location.

I’m making a simple word puzzle so this works for me.

sorry haven’t found the code still :sweat: Have some kids and they are on holidays :scream::see_no_evil:

1 Like

found it:
First add draggable behaviour to objects then when mouse button is down I check if any of the objects has been clicked

If they have, I set variable Arrastrando (dragging) to 1,If you release and press button again notice that I set the variable to 0 each time you do a click. Here I thought I could use internal variables for object or scene variables to store the original position of the object being dragged in case you release it on a place where it should.
The next event to check is mouse button released, when you release button it means you stopped dragging, so I get MouseX and MouseY coordinates and check if they are where I wanted the object to be dragged.
There is a lot more to do but it is a beggining, hope this helps

1 Like

The draggable behavior works fine - until you have a hit box that is not a rectangle and the non-hit box area overlaps another shape (see: Click and Drag with overlapping objects). I ended up creating my own function to ensure that the object clicked is the object that is dragged. If you are interested, I can share the function later tonight (right now I am on an Amtrak and the internet connection stinks). I have another function that snaps to a grid but only when the dragged piece is entirely inside the “play area”.


1 Like