A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: for each (child:someClass) in MovieClip? doesn't work!

  1. #1
    Member
    Join Date
    Jul 2009
    Location
    Florianópolis
    Posts
    81

    for each (child:someClass) in MovieClip? doesn't work!

    hello,

    In short:
    How can I remove all children that are instances of a given class?

    I've tried the following:

    Code:
    for each (var obj:InfoBox in infoPanel)
    {
         infoPanel.removeChild(obj);
    }
    if i do that i get this error:

    Code:
    TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::MovieClip@4304f821 to InfoBox
    however, if i use MovieClip instead of InfoBox (for each (var obj:MovieClip in infoPanel), it works well and it removes all movieclips.

    and if i trace infoPanel.getChildAt(i) (in a loop), i get all the objects [object InfoBox] as well as the objects MovieClips.

    Why?

    thanks in advance!
    Last edited by ziriguidum; 04-08-2010 at 01:45 AM. Reason: clarify

  2. #2
    Member
    Join Date
    Jul 2009
    Location
    Florianópolis
    Posts
    81
    so, i also tried:

    Code:
    		for each (var obj:* in infoPanel)
    		{
    			trace(" clean this!!! " + obj + ", name: " + obj.name);
    				
    			if (obj is InfoBox)
    				info.removeChild(obj)
    				
    		}
    it only traces the movieclips!! why doesn't it trace the objects InfoBox then?

    Last edited by ziriguidum; 04-08-2010 at 01:44 AM. Reason: clarification

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    for each.. in only iterates over the dynamic properties of an object. You are trying to iterate over the display children. You need to use a for loop with getChildAt and numchildren.

    Code:
    for (var i:int = 0; i < infoPanel.numChildren; i++){
      var dc:DisplayObject = infoPanel.getChildAt(i);
      if (dc is InfoBox){
        infoPanel.removeChild(dc);
        i--; //removed that child, so revisit that index.
      }
    }

  4. #4
    Member
    Join Date
    Jul 2009
    Location
    Florianópolis
    Posts
    81
    man , i really appreciate your help. exactly what i needed to know.

    THANKS!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center