0
votes

I have a 4*4 grid. I have 16 MovieClips scattered around the grid. Each MovieClip is 40*40 and they are draggable. The user can drag and drop the MovieClips into the grid. Each clip has a correct position. For example for

clip 0 : x=0 , y=0

clip 1 : x=40, y=0

clip 2 : x=120 , y=0

clip 3 : x=160 , y=0

clip 15 : x=160 , y=160

The game finishes when all the clips are in correct position. So its easy to check for game over. Use a loop and ENTER_FRAME to monitor the positions of all clips.

There is a small problem I cannot solve. When the user drags a clip it will snap to position in the grid whether it is correct or not. The snappable positions will be multiples of 40 in this case. It is possible for two clips to snapped to a location and this has to be avoided. If I have put a clip at (40,120) I should not be able drop another MovieClip there. In effect I should be able to get some indication telling me that this position is occupied.

Using the indication I will make the MovieClip return to original position.

How can this can be achieved. The registration points of MovieClips are topleft.

1

1 Answers

2
votes

Carry an object with information about the occupation of the 16 fields. If a clip is present, set the appropriate property to true. Alternative: A nifty solution is to simply use an int var and a binary sum:

1 = first field
2 = second field
4 = third field
8 = ...

If the first three positions are occupied, your sum will be 1 + 2 + 4 = 7.

You test if a field is occupied like this:

if ((SUM & testFieldNumber) == testFieldNumber) THEN occupied

At the time the user releases the mouse you need to calculate the position the clip will snap into. Check if the field is occupied using your object or the binary sum thing. If so, reset the position of the clip to its former state. If the field is still free, you add the field to your object or the binaray sum:

SUM += fieldNumber

If a clip leaves its field:

SUM -= fieldNumber

Summary:

  • Store always the last valid position of each of your clips
  • Store the occupation of the fields (object or binary sum)
  • Calculate the possible clip position (field) when the user releases the mouse
  • If field is free: Move clip there, store field occupation (in object or bin sum). Store the new position of the clip as last valid position.
  • If field is occupied: Move clip to its former position
  • When clip leaves its former position update the field occupation list (object or bin sum)