如何在 Flash/AS3 中创建网格层并识别图标放置在哪些象限中?

发布于 2024-11-08 15:07:42 字数 192 浏览 0 评论 0原文

早上好,

我有一个现有的 Flash 应用程序,其中有一个图层包含一个分为 4 个象限的正方形。向用户提供一组图标,并要求他/她将每个图标拖到一个象限中。

Flash/AS3 是否提供了一种创建覆盖现有图像的网格(不一定是 DataGrid)的方法?如果是这样,如何设置 AS3 语法来保存放置图标的象限?

非常感谢!!

Good Morning,

I have an existing Flash app that has a layer containing a square separated into 4 quadrants. A set of icons is provided to the user and he/she is asked to drag each icon into one of the quadrants.

Does Flash/AS3 provide a way to create a grid (not necessarily DataGrid) that overlays the existing image? If so, how do I setup AS3 syntax to save the quadrant in which an icon is placed?

Thanks much!!

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

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

发布评论

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

评论(1

╰沐子 2024-11-15 15:07:42

如果我正确理解你的问题,你有一个 2 x 2 网格,可以将图标拖入其中。您需要保存该网格中图标的位置。

有几种方法可以做到这一点,但我认为最简单的方法是使用一点数学来确定图标的位置。

如果有某个按钮表明用户已将图标放置在他/她想要的位置,则在该按钮的处理程序中编写代码。

下面将找到网格左上角和图标左上角之间的距离。您可以使用该距离的 x 值除以象限的宽度来查找图标上有多少个象限。在这种情况下,象限的宽度是网格总宽度的一半(因为它的宽度只有 2)。为了找出图标向下有多少个象限,请对距离的 y 分量执行相同的操作。

var col:Number = Math.round( Math.abs(icon.x - grid.x) / (grid.width/2))+1;
var row:Number = Math.round( Math.abs(icon.y - grid.y) / (grid.height/2))+1; 

Math.abs(绝对值函数)是为了防止距离因某种原因为负数。如果没有 +1,该表达式将在 colrow01 code> 如果您认为第一个象限是 0 x 0,那就没问题了,但既然我说的是 2 x 2 网格,那么最好将网格的最远角设置为 2 x 2

。工作使网格和图标都是具有实例的符号名称。单击舞台(主工作区域)上的符号后,可以在“实例名称”下的属性窗口中设置实例名称。

此外,如果您实现此功能,您会注意到,即使图标位于象限的一半以上,它仍然会注册为位于上面的象限中。这是因为在上面的代码中我们正在比较左上角之间的距离。为了测量图标中心与网格左上角之间的距离,只需将图标高度的一半添加到其 y 值,如下

var row:Number = Math.round( Math.abs((icon.y+icon.height/2) - grid.y) / (grid.width/2))+1;

所示 :还没有测试过这段代码,但理论上它应该可以工作。

If I understand your question correctly, you have a 2 x 2 grid into which icons are dragged. You need to save the position of a icon in this grid.

Couple ways to do this but I think the easiest would be to use a little bit of math to determine where the icon is.

If there is some button that indicates the user has placed the icon where he/she wants to then write your code in the handler for that button.

The following will find the distance between the top-left corner of the grid and the top-left corner of the icon. You can use this to find how many quadrants over the icon is by dividing the x value of this distance by the width of a quadrant. In this case the width of a quadrant is half the size of the grid's total width (because it is only 2 across). In order to find out how many quadrants down the icon is, do the same with the y component of the distance.

var col:Number = Math.round( Math.abs(icon.x - grid.x) / (grid.width/2))+1;
var row:Number = Math.round( Math.abs(icon.y - grid.y) / (grid.height/2))+1; 

The Math.abs (absolute value function) is just in case the distance is negative for whatever reason. Without the +1 that expression would result in either 0 or 1 in col and row which would be fine if you considered the first quadrant to be 0 x 0 but since I said 2 x 2 grid it'd be nice to have the furthest corner of the grid to be 2 x 2.

Also in order for this to work make the grid and the icon are both symbols with instance names. Instance names can be set in the properties window under "instance name" after clicking on the symbol on the stage (main work area).

Furthermore should you implement this you'll note that even if the icon is more than half way down a quadrant it will still register as being in the quadrant above. This is because in the code above we're comparing the distance between the top left corners. In order to measure the distance between the center of the icon and the top left corner of the grid simply add half the height of icon to its y value like so:

var row:Number = Math.round( Math.abs((icon.y+icon.height/2) - grid.y) / (grid.width/2))+1;

Note I haven't tested this code but in theory it should work.

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