Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Contributor ,
May 10, 2008 May 10, 2008
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.
TOPICS
ActionScript
326
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 10, 2008 May 10, 2008
LATEST
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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines