Flash 拼图游戏中拼图块的位置

发布于 2024-12-02 03:52:30 字数 518 浏览 0 评论 0原文

我有一个4*4的网格。我有 16 个 MovieClip 分散在网格中。每个MovieClip都是40*40并且可以拖动。用户可以将影片剪辑拖放到网格中。每个剪辑都有正确的位置。例如,对于

剪辑 0:x=0,y=0

剪辑 1:x=40,y=0

剪辑 2:x=120,y=0

剪辑 3:x=160,y=0

剪辑 15:x=160, y=160

当所有剪辑都处于正确位置时游戏结束。因此很容易检查游戏是否结束。使用循环和 ENTER_FRAME 监视所有剪辑的位置。

有一个小问题我无法解决。当用户拖动剪辑时,无论正确与否,它都会捕捉到网格中的位置。在这种情况下,可捕捉位置将为 40 的倍数。两个剪辑可能会捕捉到一个位置,必须避免这种情况。如果我在 (40,120) 处放置了一个剪辑,我就不应该再在那里放置另一个 MovieClip。实际上,我应该能够得到一些指示,告诉我这个位置已被占用。

使用指示我将使影片剪辑返回到原始位置。

这怎样才能实现呢。影片剪辑的注册点位于左上角。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没︽人懂的悲伤 2024-12-09 03:52:30

携带一个带有16个字段占用信息的物体。如果存在剪辑,请将适当的属性设置为 true。替代方案:一个巧妙的解决方案是简单地使用 int var 和二进制 sum:

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

如果前三个位置已被占用,则总和将为 1 + 2 + 4 = 7

您可以这样测试字段是否被占用:

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

当用户释放鼠标时,您需要计算剪辑将捕捉到的位置。使用您的对象或二进制总和检查该字段是否被占用。如果是这样,请将剪辑的位置重置为其之前的状态。如果该字段仍然空闲,则将该字段添加到对象或二进制和中:

SUM += fieldNumber

如果剪辑离开其字段:

SUM -= fieldNumber

摘要:

  • 始终存储每个剪辑的最后一个有效位置
  • 存储字段的占用(对象或二进制和) )
  • 计算用户释放鼠标时可能的剪辑位置(场)
  • 如果场是空闲的:将剪辑移动到那里,存储场占用(在对象或 bin sum 中)。将剪辑的新位置存储为最后一个有效位置。
  • 如果字段被占用:将剪辑移至其先前位置
  • 当剪辑离开其先前位置时更新字段占用列表(对象或 bin 总和)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文