• Coding
  • [AS3] Not All Children Are Being Removed

Hey everyone,

I'm having some issues with removing the children of a Sprite. Below is my code:
private function onTitleClick(e:MouseEvent):void {
			var proj:Project = e.currentTarget as Project;
			var images:XMLList = proj.images;
			_counter = 0;
			_thumbCount = images.length();
			_bottomContainer.alpha = 0;
			clearBottomCont();
			for (var i = 0; i < _thumbCount; i++) {
				var thumb:ProjectThumb = new ProjectThumb(images[i].@thumb, images[i].@url);
				thumb.addEventListener(Event.COMPLETE, thumbLoaded);
			}
		}
		
		private function thumbLoaded(e:Event):void {
			var thumb:ProjectThumb = e.target as ProjectThumb;
			thumb.x = (120 + 10) * _counter;
			_bottomContainer.addChild(thumb);
			_counter++;
			if (_counter == _thumbCount) {
				trace(_counter);  //outputs the right value
				trace(_thumbCount);  //outputs the right value
				trace(_bottomContainer.numChildren);  //doesn't always output the right value (it should output the same value as _thumbCount)
				_bottomContainer.x = stage.stageWidth;
				_bottomContainer.y = stage.stageHeight - 120;
				TweenLite.to(_bottomContainer, 0.7, { x: (stage.stageWidth - _bottomContainer.width), alpha:1 } );
			}
		}
		
		private function clearBottomCont():void {
			if (_bottomContainer.numChildren > 0) {
				for (var i:uint = 0; i < _bottomContainer.numChildren; i++)
					_bottomContainer.removeChildAt(i);
			}
		}
So what's going on and how can I solve this problem?
while(sprite.numChildren > 0)
   sprite.removeChildAt(0);
You're just skipping elements by removing at index i.

Alternate method.
var count:int = sprite.numChildren;
for(var i:int = 0; i < count; i++)
   sprite.removeChildAt(0);
Explanation:
list: 0 1 2 3 4 5 / num = 6
ptr:  ^ = 0

list: 1 2 3 4 5 / num = 5
ptr:    ^ = 1 "i" is your pointer, and here it moved past the first element
Ah right! I keep having this issue over and over again. The first method solved the problem. Thanks a lot :)
		private function clearBottomCont():void {
			while (_bottomContainer.numChildren > 0)
				_bottomContainer.removeChildAt(0);
		}