2 Replies Latest reply on Jun 19, 2011 5:25 AM by bradt8

    problem with getChildByName

    bradt8

      I'm tying to remove some objects by name but I get TypeError: Error #2007: Parameter child must be non-null.

      I dont see why - I need to remove some objects by name but cant so I wrote in some code to get object names so I know they exist if I run code with the remove comented out

       

      var num:uint = fieldSprite.numChildren;

      for (i = 0; i < num; i++) {

      trace(fieldSprite.getChildAt(i).name);

      var st:String = fieldSprite.getChildAt(i).name;

      trace(fieldSprite.getChildByName(st));

       

      //fieldSprite.removeChild(getChildByName(st));

      }

       

      I get output -

       

      16_4

      [object Sprite]

      17_4

      [object Sprite]

      18_4

      [object Sprite]

      19_4

      [object Sprite]

       

      but if I run it with out the removeChild commented out I get -

       

      16_4

      [object Sprite]

      TypeError: Error #2007: Parameter child must be non-null.

      at flash.display::DisplayObjectContainer/removeChild()

      at Main/landStake()

      at Main/onTimer()

      at flash.utils::Timer/_timerDispatch()

      at flash.utils::Timer/tick()

       

      if I change the code to us a with statment -

       

      var num:uint = fieldSprite.numChildren;

      for (i = 0; i < num; i++) {

      trace(fieldSprite.getChildAt(i).name);

      var st:String = fieldSprite.getChildAt(i).name;

      trace(fieldSprite.getChildByName(st));

      with (fieldSprite) {

      removeChild(getChildByName(st));

      }

      }

       

      I get -

       

      16_4

      [object Sprite]

      18_4

      [object Sprite]

      RangeError: Error #2006: The supplied index is out of bounds.

      at flash.display::DisplayObjectContainer/getChildAt()

      at Main/landStake()

      at Main/onTimer()

      at flash.utils::Timer/_timerDispatch()

      at flash.utils::Timer/tick()

       

      two of the objects get removed but two dont.

       

      Anyone know what I'm doing wrong?

       

      thanks

        • 1. Re: problem with getChildByName
          Ned Murphy Adobe Community Professional & MVP

          It is most likely because you are counting upwards in the for loop.  When you remove an object, it doesn't leave a vacancy where it was in the display list... everything shifts down.  So when you're at the halfway mark of your count you are probably already beyond any "childAt" location since everything shifted to fill the holes...  You essentially only remove every other one of the objects when you count up.  Try going backwards and remove the objects from the numChildren-1 thru 0 instead.

          • 2. Re: problem with getChildByName
            bradt8 Level 1

            Ned,

             

            Thanks for that that got it working but I had to remove the fieldSprite container as well. Done really see why that is an issue?