Results 1 to 9 of 9

Thread: please help with remove child in AS3

  1. #1

    please help with remove child in AS3

    Hi

    I'm using an Array to addChild a movieclip to with a loop. The problem is, is I don't know where the addChild is putting the object. When I try to reference it on the stage, I get errors. What I want is when I click on one of the buttons (newCircle) for to set my variable (which it does) and remove all instances of newCircle from the stage. The relevant code is almost at the very end if someone can take a look:

    package
    {
    import flash.display.MovieClip;
    import divieoEventButton;
    import Boarder;
    import flash.events.MouseEvent;
    import flash.xml.XMLDocument;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;

    public class divieoKiosk extends MovieClip
    {
    private var newCircle:divieoEventButton;
    private var newCircleX:Number;
    private var newCircleY:Number;
    private var divieventname:Array;
    private var _firstCard:*;

    public function divieoKiosk()
    {
    divieventname = new Array();
    createCards();
    }

    private function createCards():void
    {
    var xml:XML = new XML(); //setup initial vars to retreive xml data
    var loader = new URLLoader();
    loader.load(new URLRequest("divieoconfig2.xml")); //xml file is local to swf
    loader.addEventListener(Event.COMPLETE, function(evt:Event):void //wait for xml to load
    {
    xml = XML(evt.target.data);
    newCircleX = 210;
    newCircleY = 400;
    var num:Number = xml.divievent.length();

    for (var i:Number = 0; i < num; i++) //start a loop to build out movie clip/buttons
    {
    divieventname[i] = xml.divievent[i].@honornames.toXMLString();

    newCircle = new divieoEventButton();
    addChild(newCircle);

    if (newCircleX < 1300) { //add movie clips as long as they don't go off screen
    newCircle.x = newCircleX;
    newCircle.y = newCircleY;
    newCircleX += newCircle.width + 10; //add 10 pixels to the width of each clip
    }
    else { //if movie clips do off screen, then reset X and add 10 pixels to the height
    newCircle.x = newCircleX;
    newCircle.y = newCircleY;
    newCircleX = 210;
    newCircleY += newCircle.height + 10;
    }
    newCircle.addEventListener(MouseEvent.CLICK, checkCards);
    divieventname.push(newCircle);

    newCircle.txthonornames.text = divieventname[i];

    }
    }
    )
    };

    private function checkCards(event:MouseEvent):void
    {
    _firstCard = event.currentTarget.txthonornames.text;
    trace(_firstCard);
    //Here is where my removeChild should be
    }
    }
    }

  2. #2
    try
    Code:
    event.target.parent.removeChild(event.target);

  3. #3
    thanks!!

    this is what worked for me:

    event.currentTarget.parent.removeChild(event.curre ntTarget);

  4. #4
    I spoke too soon. This code removes the single instance of the movie clip, but not all instances.

    I'm trying to make this work, but it only removes the movie clips that have a higher index than the one I'm clicking on.

    Code:
    while (event.currentTarget.parent.numChildren > 0) {
    				event.currentTarget.parent.removeChildAt(event.currentTarget.parent.numChildren-1);
    }

  5. #5
    maybe im confused on what you are trying to do, if you click on newCircle and want to get rid of all the circles in the array try

    Code:
    for(var i:int = 0;i<divieventname.length();i++) {
            if (divieventname[i] == null) { continue; }
            divieventname[i].parent.removeChild(divieventname[i]);
    }

  6. #6
    Yeah, I want the child clicked on along with all of it's brothers and sisters to go away once one of them is clicked on. This is for a kiosk application, btw.

    When I plugged in your for statement from above, I get this error:

    Error #1006: value is not a function.


    any other thoughts?
    Thank You so much for your help.

  7. #7
    Ok, I've simplified this entire thing. But I don't know how to add an event listenenr to each button so that I can click and remove all of it's parent's children.

    Code:
    var xml:XML = new XML(); //setup initial vars to retreive xml data
    var loader:URLLoader = new URLLoader();
    loader.load(new URLRequest("divieoconfig2.xml")); //xml file is local to swf
    loader.addEventListener(Event.COMPLETE,	function(evt:Event):void //wait for xml to load
    		{
    		xml = XML(evt.target.data);
    		var newCircleX:Number; //establish starting point for movie clips to appear
    	    	var newCircleY:Number;
    		
    		var num:Number = xml.divievent.length(); 
      		var divieventname:Array = new Array(); 
    		newCircleX = 200;
    		newCircleY = 400;
    		
      			for (var i:Number = 0; i < num; i++) //start a loop to build out movie clip/buttons
    			{
    			divieventname[i] = xml.divievent[i].@honornames.toXMLString();
    			
    	    	var newCircle:mceventlist = new mceventlist(); //link to movie clip in library
    			if (newCircleX < 1300) { //add movie clips as long as they don't go off screen
    				newCircle.x = newCircleX;
    				newCircle.y = newCircleY;
    				newCircleX += newCircle.width + 10; //add 10 pixels to the width of each clip
    				}
    				else { //if movie clips do off screen, then reset X and add 10 pixels to the height
    				newCircle.x = newCircleX;
    				newCircle.y = newCircleY;
    				newCircleX = 200;
    				newCircleY += newCircle.height + 10;
    			}
    		addChild(newCircle); //add child to movie clip instance
    		newCircle.txthonornames.text = divieventname[i]; //take current array position and assign it to the text box in my movie clip
    	  		 }
    		}
    
    );
    stop();

  8. #8
    I'm presuming this is a similar problem to what I was having. If you're looping through a DisplayObject and are basing the loop on numChildren, then not all of the children are removed as the value of numChildren changes as you remove the children... if you get my drift!?

    The way I remove all children is like so:

    Code:
    var count:uint = mySprite.numChildren;
    for(var i:uint=0;i<count;i++){
        mySprite.removeChildAt(0);
    }
    Use the 0 (zero) index for removeChildAt as everytime you remove an item, it shifts everything in the display list down by 1.

  9. #9
    Or use...
    while (mySprite.numChildren) mySprite.removeChildAt(0);

    the common way is to iterate backward through an array if you need to modify its structure.

    for (i=mySprite.numChildren; i>0 ; i--) //etc...

    So... To remove siblings too use


    Code:
     
    var p=mySprite.parent;
    
    while(p.numChildren) p.removeChildAt(0);
    Last edited by scooby56; March 12th, 2008 at 06:12 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012