Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As discussed in the comments to the initial question, finding the touch location from the container view seems best. The biggest bottleneck in this code is storing all your objects in one linear array, then checking each element in your array. A better method would be to store the objects in a better format where you can access individual objects based on their position. </p> <p>If your objects are arranged on a strict grid, then storing them in a two dimensional array should be fast enough. Subdivide your screen into the grid divisions, then find the touchesMoved point within those grid divisions. Use those two integers as indexes to your 2D array, and check the one object available at that location in your array. If you don't have an object at every position on the grid, just store a nil at that location to tell your code "No need to check here". If you may have more than one object at each grid location you can keep a list of the objects at that location in the array, and check all objects in the list. This will be much less than the total (if your objects don't clump up). </p> <p>Even if your objects are not arranged on a strict grid you can use this technique. Merely dividing your screen into 4 quadrants will reduce the time required to check your objects roughly to one fourth the initial time. </p> <p>If your objects are moving you'll have to keep this array updated. </p> <p>This 2D array is just my first thought on a better way of doing this, based on the limited information in your question. It may not be the best way to work in your actual program, and a different data structure and algorithms may be better suited. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload