3 Replies Latest reply on Aug 2, 2007 12:44 PM by Juankpro

    Effect of Splice() on Objects Stored in Arrays

    El_Tigre_Naranja
      I'm having trouble with storing custom objects in a multidimensional array. The structure is a number of 2 index long arrays stored in a containing array. The objects work as expected until I need to rearrange the containing array. getName() is a custom method.

      <---
      private function moveRow(ar_startingIndex:Number):Void{
      trace("name of first thumb: " + this.ar_thumbs[0][0].getName());
      var v_tempArray = this.ar_thumbs.splice(ar_startingIndex, 1);
      trace("first thumb in v_tempArray: " + v_tempArray[0][0]);
      this.ar_thumbs.unshift(v_tempArray);
      trace("name of first thumb is now: " + this.ar_thumbs[0][0].getName());

      }
      --->

      The first trace returns the name of the object. The other two return "undefined" Can someone help me figure out how to rearrange the containing array without losing the functionality of the contained objects?
        • 1. Re: Effect of Splice() on Objects Stored in Arrays
          Juankpro
          Tried a simple sample on my computer and it works fine except that this line was wrong: replace ths

          this.ar_thumbs.unshift(v_tempArray);

          with this

          this.ar_thumbs.unshift(v_tempArray[0]);

          But because the problem begins in a line before that one I don't know what the problem might be
          • 2. Re: Effect of Splice() on Objects Stored in Arrays
            El_Tigre_Naranja Level 1
            My second trace was wrong. It should have been trace("first thumb in v_tempArray: " + v_tempArray[0]), since v_tempArray is not a multidimensional array. This script gets ran multiple times. It works (partly) the first time, but as soon as the splice is made, the objects contained in the spliced v_tempArray, and in the unshifted this.ar_thumbs[0] don't work anymore.

            Here's the result of the traces for a couple of iterations:

            moveThumbRow is running
            targetY: 0
            name of first thumb: 1. Amherst
            first thumb in v_tempArray: _level0.instance1.mc_thumbBlock.cabinet_50,_level0.instance1.mc_thumbBlock.cabinet_54
            name of first thumb is now: undefined
            moveThumbRow is running
            targetY: undefined
            name of first thumb: undefined
            first thumb in v_tempArray: _level0.instance1.mc_thumbBlock.cabinet_34,_level0.instance1.mc_thumbBlock.cabinet_38
            name of first thumb is now: undefined
            • 3. Effect of Splice() on Objects Stored in Arrays
              Juankpro Level 2
              The second trace is OK, don't forget that the splice function do not return the removed element. Because splice can remove many elements, it creates an array of removed elements so, when you remove splice creates an array with one element, and this element is your array. So it is ok to access using double brakets.