Skip to main content
Inspiring
May 10, 2008
Question

How to loop through a range of an array (based on element value)

  • May 10, 2008
  • 1 reply
  • 352 views
How to loop through a range of an array (based on element value)

I'm looping through an array doing some collision detection:

for (var n:int = 0; n<myArray.length; n++){
//do collision detection
trace(myArray.name);
trace(myArray.x);
}


Now I want to do the same thing, but I only want to loop through a subset of the array (since I don't want to waste resources doing collision detection on offscreen objects.

So I want to loop through the section of the array where the attribute X falls into a certain value.

Could someone please help me do that?

My array is sorted by X.
This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 10, 2008
assuming attribute x is a number and you're looking for elements that have an x attribute within a certain range and you have, at least, a few hundred array elements, you can speed your collision detection by reordering your array based on elements x attribute (check the sort method() ) and using a binary search routine for the starting array element.