找到最接近鼠标位置的点 [英] find closest point to mouse position
问题描述
现在我想在一个网格元素上拖动图像。
因此,我需要知道网格元素的哪个x / y是最接近鼠标位置的点。
所有的网格元素都存储在一个数组中。
如何才能做到这一点?
如果您只需要最接近的网格的x,y,那么所有你必须这样做。
var gridX:int = Math.floor(mouseX / NumberOfColumns);
var gridY:int = Math.floor(mouseY / NumberOfRows);
这会将您的鼠标坐标转换为网格坐标。
现在出现这个问题。如果将它们存储在一个2d数组中,那么如果将它们存储在一个平面数组(1d)中,则需要按照您创建它的方式查找它。
var myObject:Object = my2dArray [gridX,gridY];
var myObject:Object = myFlatArray [(gridX * NumberOfRows)+ gridY];
如果您已经考虑过如何创建数组并将它们推入其中,那应该是没有问题检索的东西没有搜索它。
I've got a grid of sprites. Now I would like to drag an image on a grid-element. Therefore I need to know which x/y of the grid-element is the closest point to the mouse-position. All the grid-elements are stored in an array. How can I achieve that?
If all you need is the x,y of the closest grid then all you have to do is.
var gridX:int = Math.floor(mouseX / NumberOfColumns);
var gridY:int = Math.floor(mouseY / NumberOfRows);
This will convert your mouse coordinates to your grid coordinates.
Now comes the problem. If your storing them in a 2d array then you have your x/y if your storing them in a flat array (1d) you need to look it up just like you created it.
var myObject:Object = my2dArray[gridX, gridY];
var myObject:Object = myFlatArray[(gridX * NumberOfRows) + gridY];
If you have taken care of how you create your array and push the items in it, it should be no problem to retrieve stuff without searching it.
这篇关于找到最接近鼠标位置的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!