Good afternoon. I have a task to make a mini-game with drag’n’drop. The codepen link shows an example of a base (3 squares of different colors):
I need some advice. I know how to do the drag itself. But I can’t think of how to do the following yet:
- Limit the dragging area (so that the square does not go off the screen). As I understand it, for this, when dragging, I need to check the distance to the edges of the screen. And if the distance is less than the size of the square, then block dragging, right?
- How do I swap squares? For example, I grab and drag the first square (blue in codepen). If it overlaps the next square (for example, aqua), then they should swap places. For example, like when you change the order of the music in a playlist
- Determining the correct order. In the mini-game, you need to arrange the squares in a certain order, for example, gold, aqua and blue. What is the best way to check the order of these squares when you finish dragging? For example, the player dragged the blue square to the place of the gold one and then you need to check whether the order is correct