2 Replies Latest reply on Oct 14, 2007 3:24 PM by Rothrock

    Delete stuff in array

    OmniFlash
      I got 10 movie clips in an array

      a movieclip tests every movieclips for an hitTest

      for (var i in Array)

      Now what I do is removeChild

      Followed by a splice removing the unwanted element)

      The thing is:
      Splice makes my Array unusable because the list changed slightly and was realocated or somethng

      So how do I delete like 5 movieClips AT ONCE because I dont want to do a for 5 time to delete 5 clips
        • 1. Re: Delete stuff in array
          clbeech Level 3
          I'm sorry Omni, this makes very little sense. What five MCs would you like to remove? the code 'for(var i in Array)' is incorrect and not how you run a hitTest on elements in an Array. 'Array.splice(i)' will remove an element from that index of an array OR can remove 'multiple' elements from the array when passed the start and ending parameters.

          So if for instance, you wanted to remove elements from the array beyond the one the was determined as 'hit' by the hitTest, then use that index as the start param and then add five to it, IF the elements you wish to remove are in order within the array.

          OR if the elements you wish to remove are specific, given a condition, then you could list them in a different array, and test to see if the elements match after the hitTest is confirmed, then remove them from the array if so.

          But this is all speculation, it is very hard to understand what you're trying to do here from your description.
          • 2. Re: Delete stuff in array
            Rothrock Level 5
            Not really understanding what you mean, but I might have an idea.

            Splice doesn't make arrays unusable it does just what it says adds/removes elements from an array. So if your array is becoming unusable then there must be something else going on. Also "unusable" isn't really a very precise notion. How is is becoming unusable?

            My guess is that you are removing early elements in the array and then running out of elements or what was element 5 is now element 4 or something similar to that. Maybe that is what is happening?

            So instead of using var i in Array, perhaps this approach would be better.

            for(var i=myArray.length-1; i>=0;i--){
            // check for colision
            // remove element
            // etc.
            }

            By working backward through the array you won't be trying to hit a moving target so to speak.

            Or perhaps there is something I'm missing?