0
How to loop through a range of an array (based on element value)
Contributor
,
/t5/animate-discussions/how-to-loop-through-a-range-of-an-array-based-on-element-value/td-p/962791
May 10, 2008
May 10, 2008
Copy link to clipboard
Copied
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.
I'm looping through an array doing some collision detection:
for (var n:int = 0; n<myArray.length; n++){
//do collision detection
trace(myArray
trace(myArray
}
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
LATEST
/t5/animate-discussions/how-to-loop-through-a-range-of-an-array-based-on-element-value/m-p/962792#M25877
May 10, 2008
May 10, 2008
Copy link to clipboard
Copied
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.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

