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?
相关问题
- garbage collection best practices
- Tkinter Grid Columnspan ignored
- How to load flex swf from flash?
- How to get a fixed number of evenly spaced points
- FlashDevelop Haxe (Flash) debugger
相关文章
- Numpy matrix of coordinates
- Are there any benefits when using final in AS3?
- Trace on Chrome/Browser console
- as3 ByteArray to Hex (binary hex representation)
- Powershell - Close Out-GridView after specific tim
- Leaflet: how to swap coordinates received from an
- About Collision detection alghorithms in AS3
- How to upload BitmapData to a server (ActionScript
You must loop through all the elements and find the smallest distance to the mouse. Then store the array index of the element. Try something like this:
Of course, this will only get you the first closest element, so you must decide what you want to happen if there are two elements an equal distance away.
You will also need to allow for the origins of your elements. The will probably have there origins set to there top left, so you need to allow for this in your distance calculation.
You could also check for a minimum distance. So if the user is dragging too far away from any of the elements then do nothing.
If all you need is the x,y of the closest grid then all you have to do is.
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.
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.