AS3: Removing children of children
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: AS3: Removing children of children

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269

    AS3: Removing children of children

    I have created a container movieclip called gameCanvas which I've added to the stage. I then have a large number of movieclips which have been added as children of gameCanvas. The reason I did this is because I thought that I would be able to get rid of all these children in one go by removing gameCanvas. Obviously I was wrong! gameCanvas is removed successfully, but all the child movieclips remain.

    Is the only way to remove all the children of a container movieclip to loop through each one of them in a for loop?

    And what happens to all those orphaned children - do they become children of their grandparent - ie. the parent of gameCanvas (in my case, the stage)?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Post some code. Removing a DisplayObject from the displayList should remove all its children as well, so you may not have added them to the thing you think you did.

  3. #3
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    Oh! That's what I thought should happen. I must have done something wrong then - here's what I did:

    Code:
    //create container movieclip and add to stage - this is done outside of any functions, so the variable is global
    var gameCanvas:MovieClip  = new MovieClip();
    stage.addChild(gameCanvas);
    
    //Then, inside a function, I add children to container like this:
    gameCanvas.addChild(myMovieClip);
    
    
    //and in another function, I remove container:
    stage.removeChild(gameCanvas);
    Anything wrong here? Ultimately, it's not a massive deal, as I can loop through all of the children removing them individually.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    No, in theory, that's correct. One thing to be on the lookout for is if you ever add the same DisplayObject to another parent. If you do that, it'll be removed from the one it was on before and re-parented.

    Here's a bit of code that proves that children are removed with parents:
    Code:
    package
    {
    	import flash.display.Sprite;
    	import flash.events.MouseEvent;
    	import flash.text.TextField;
    	
    	public class Main extends flash.display.Sprite
    	{
    		private var container:Sprite;
    		public function Main():void
    		{
    			container = new Sprite();
    			addChild(container);
    			
    			var tf:TextField = new TextField();
    			tf.text = "in container";
    			container.addChild(tf);
    			tf.addEventListener(MouseEvent.CLICK, removeContainer);
    		}
    		
    		private function removeContainer(event:MouseEvent):void {
    			removeChild(container);
    		}
    	}
    }

  5. #5
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    Weird. No - the objects weren't added to any other parent. I guess there must be something odd happening somewhere else in my code, but there's too much of it to ask anyone to scour through it. Thanks for helping anyway - I'll just remove the children by looping through them which seems to work fine.

    Cheers.

  6. #6
    Junior Member
    Join Date
    Apr 2008
    Posts
    3
    I think i have about the same problem in AS3 over here. I'm trying to assign instances of the RpButton class to _menuHolder which i use as a container. Works perfectly but i'd like to replace all the buttons for new ones when an other menu is loaded, so i thought i should just remove _menuHolder in an IF statement and then run the same function again but somehow _menuHolder in the IF statement is an null-object.... 8(
    Hope you guys can help me out.... BTW it's XML-based

    the code:

    =================================

    package
    {
    import flash.display.*;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import RpMenu;
    import RpButton;
    import flash.xml.*;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import fl.transitions.easing.*;
    import fl.transitions.*;


    public class RP extends MovieClip
    {
    private var xmlLoader:URLLoader;
    public var xml:XML;
    private var projectPath:*;
    private var iProjectLength:Number;
    private var iBtnLength:Number;
    private var menuLoaded:Boolean = false;
    private var main = this;

    public function RP()
    {
    xmlLoader = new URLLoader();
    xmlLoader.load(new URLRequest("content.xml"));
    xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

    function xmlLoaded(event:Event):void
    {
    xml = XML(event.target.data);
    }

    _concept.addEventListener(MouseEvent.CLICK, onClickConcept);
    _performance.addEventListener(MouseEvent.CLICK, onClickPerformance);
    _shortFilm.addEventListener(MouseEvent.CLICK, onClickShortfilm);

    }
    // MENU MAKEN

    private function maakMenu()
    {

    if (menuLoaded == true)
    {
    trace("menuLoaded is true");
    trace (_menuHolder); returns nulll?????
    trace (this._menuHolder); returns nulll?????
    trace (root._menuHolder); returns nulll?????

    }

    var _menuHolder:menuHolder = new menuHolder();
    _menuHolder.name = "_menuHolder";
    //var menuHolder:MovieClip = new MovieClip();
    var _RpButton:RpButton;
    iProjectLength = projectPath.length();
    var _RpButtonX = 0;
    var _RpButtonY = 0;

    for(var iProject:Number = 0; iProject < iProjectLength; iProject++)
    {
    iBtnLength = projectPath[iProject].sub.length();
    var subPath:* = projectPath[iProject].sub

    for(var iBtn:Number = 0; iBtn < iBtnLength; iBtn++)
    {
    _RpButton = new RpButton();
    _RpButton.x = _RpButtonX;
    _RpButton.y = _RpButtonY;
    _RpButton.loadImage(subPath[iBtn].@img);
    _menuHolder.addChild(_RpButton);
    _RpButtonX += _RpButton.width + 10;
    _RpButton.addEventListener(MouseEvent.CLICK, loadFull);

    }
    //var xdist:Number;
    //this.addEventListener(MouseEvent.ROLL_OVER, scrollMenu);
    //this.addEventListener(MouseEvent.ROLL_OUT, scrollOff);
    }
    stage.addChild(_menuHolder);
    trace(_menuHolder.name);
    // this.removeChild(_menuHolder); Function is switched of but somehow over here it works!!!!!!
    menuLoaded = true;

    }

    private function loadFull(event:MouseEvent)
    {
    trace("loadFUll");
    }

    // BUTTONS

    private function onClickConcept(event:MouseEvent)
    {
    projectPath = xml.concept.project;
    maakMenu();
    }

    private function onClickPerformance(event:MouseEvent)
    {
    projectPath = xml.performance.project;
    maakMenu();
    }

    private function onClickShortfilm(event:MouseEvent)
    {
    projectPath = xml.shortfilm.project;
    maakMenu();
    }

    }
    }

  7. #7

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

Poll by Flashkit.com