Ultrashock Forums > Flash > ActionScript
AS3 - removeChildAt cant get it working properly

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
AS3 - removeChildAt cant get it working properly
Old 2008-01-23

Hi guys i did search this to get my answer but still now joy.
ActionScript Code:
  1. function logout() {
  2.     trace("holderc:"+holderc.numChildren);
  3.     var i:Number=holderc.numChildren;
  4.     while (i--) {
  5.         holderc.removeChildAt(i);
  6.         trace("holder i:"+i)
  7.     }
  8. }
It only removes one child from the stage and not all of them.
Anyone got any idea what i am doing wrong?

thanks
postbit arrow 16 comments | 5072 views postbit arrow Reply: with Quote   
Registered User
--DJD-- is offline
seperator
Posts: 564
2002-09-12
Age: 34
--DJD-- lives in United Kingdom
seperator

Ultrashock Member Comments:
spooky's Avatar spooky spooky is offline spooky lives in United States 2008-01-23 #2 Old  
Use "try/catch" on parts of your script that executes before the "logout" function.

Might be some silent error happening somewhere, preventing your script from finishing.
Reply With Quote  
sims11tz's Avatar sims11tz sims11tz is offline sims11tz lives in United States 2008-01-23 #3 Old  
hmmm what does holderc.numChildren trace out as? maybe try var i:Number=holderc.numChildren-1;
Reply With Quote  
--DJD-- --DJD-- is offline --DJD-- lives in United Kingdom 2008-01-23 #4 Old  
Sorry bud try/catch? I only started with AS3 yesterday but i have been doign AS2 for years. Can you explain how i could use try/catch.

This is really bugging me. it the last piece of the puzzle in completing my first AS3 project.

Thanks for any helps
Reply With Quote  
--DJD-- --DJD-- is offline --DJD-- lives in United Kingdom 2008-01-23 #5 Old  
Quote: Originally Posted by sims11tz View Post
hmmm what does holderc.numChildren trace out as? maybe try var i:Number=holderc.numChildren-1;
trace's
5
4
3
2
1
0
Reply With Quote  
--DJD-- --DJD-- is offline --DJD-- lives in United Kingdom 2008-01-23 #6 Old  
ok this is wierd
I tried
ActionScript Code:
  1. i:Number=holderc.numChildren-1;
This removed 6 objects from inside the mc attached and not the attached mc it's self.
Worse still 3 of these objects are just drawn vecotrs.
Reply With Quote  
spooky's Avatar spooky spooky is offline spooky lives in United States 2008-01-23 #7 Old  
This may help you keep from removing the DisplayObjects you intend to keep.
Here this will only remove Sprites.
Code:
if(holderc.getChildAt(i) && holderc.getChildAt(i) is Sprite){				
	holderc.removeChildAt(i);				
}
Reply With Quote  
--DJD-- --DJD-- is offline --DJD-- lives in United Kingdom 2008-01-23 #8 Old  
Quote: Originally Posted by spooky View Post
This may help you keep from removing the DisplayObjects you intend to keep.
Here this will only remove Sprites.
Code:
if(holderc.getChildAt(i) && holderc.getChildAt(i) is Sprite){				
	holderc.removeChildAt(i);				
}
I did try is Sprite and it did nothing and i changed it to displayObject and it did the same thing again and removed objects from within the mc i am trying to remove rather than the mc it's self.
Reply With Quote  
spooky's Avatar spooky spooky is offline spooky lives in United States 2008-01-23 #9 Old  
Quote: Originally Posted by -[DJD]- View Post
I did try is Sprite and it did nothing and i changed it to displayObject and it did the same thing again and removed objects from within the mc i am trying to remove rather than the mc it's self.
Not sure what you intend but, If you want to remove "holderc":
holderc.parent.removeChild(holderc);
Reply With Quote  
--DJD-- --DJD-- is offline --DJD-- lives in United Kingdom 2008-01-23 #10 Old  
This is how i am attaching mc's from the libarary ontot the stage.
ActionScript Code:
  1. var nextcY:Number = 466;
  2. var counCount:Number = data_lv.countryCount;
  3. var holderc:MovieClip;
  4. for (var countC:Number=0; countC<counCount; countC++) {
  5.     holderc = makeListCountry();
  6.     addChild(holderc);
  7.     holderc.x=12;
  8.     holderc.y = nextcY;
  9.     holderc.txCountry.text = data_lv['country'+countC+'country'];
  10.     holderc.txPerc.text = data_lv['country'+countC+'countryPercent']+" %";
  11.     holderc.txCount.text = data_lv['country'+countC+'countryCount'];
  12.     holderc.mc.scaleX = Math.round(data_lv['country'+countC+'countryPercent'])/100;
  13.     nextcY += 24;
  14. }
  15.  
  16. function makeListCountry():MovieClip {
  17.     var myObject:mcCountry = new mcCountry ();
  18.     return myObject;
  19. }
So what i want to do is then remove the mc's that have just been attached.
For some reason my script removes objects from within the attached mc and not the mc it's self.
Reply With Quote  
sims11tz's Avatar sims11tz sims11tz is offline sims11tz lives in United States 2008-01-23 #11 Old  
you are adding the children to your document classes display list.... so they must be removed from the same display list:

Code:
      function logout() {
          var i:Number=this.numChildren-1;
          while (i--) {
              removeChildAt(i);
          }
      }
Reply With Quote  
--DJD-- --DJD-- is offline --DJD-- lives in United Kingdom 2008-01-23 #12 Old  
Quote: Originally Posted by sims11tz View Post
you are adding the children to your document classes display list.... so they must be removed from the same display list:

Code:
      function logout() {
          var i:Number=this.numChildren-1;
          while (i--) {
              removeChildAt(i);
          }
      }
That just totally wiped my stage clear of everything.
Reply With Quote  
sims11tz's Avatar sims11tz sims11tz is offline sims11tz lives in United States 2008-01-23 #13 Old  
doh, thats what I thought you were going for. I am officially now lost at what you are trying to accomplish. So you just want to remove all of the holderc that are added to the stage? If so you can push them into an array after you instantiate them from the library. Than loop through that array and remove all items in it.... ? If that isn't it.. what exactly are you going for?
Reply With Quote  
wraevn's Avatar wraevn wraevn is offline wraevn lives in United States 2008-01-23 #14 Old  
Try this when adding to the stage:

ActionScript Code:
  1. for (var countC:Number=0; countC < counCount; countC++) {
  2.     holderc=makeListCountry();
  3.              holderc.name = "holderc_" + countC;
  4.     addChild(holderc);
  5.     holderc.x=12;
  6.     holderc.y=nextcY;
  7.     holderc.txCountry.text=data_lv['country' + countC + 'country'];
  8.     holderc.txPerc.text=data_lv['country' + countC + 'countryPercent'] + " %";
  9.     holderc.txCount.text=data_lv['country' + countC + 'countryCount'];
  10.     holderc.mc.scaleX=Math.round(data_lv['country' + countC + 'countryPercent']) / 100;
  11.     nextcY+= 24;
  12. }

(added name)

Then for your removal script:

ActionScript Code:
  1. function logout() {
  2.     var i:Number=this.numChildren-1;
  3.     while (i--) {
  4.         if(getChildAt(i).name.indexOf("holderc_") == 0){
  5.             removeChildAt(i);
  6.         }
  7.     }
  8. }
Reply With Quote  
--DJD-- --DJD-- is offline --DJD-- lives in United Kingdom 2008-01-23 #15 Old  
wraevn i owe you a pint Let me know when your in Glasgow
ok so since discovered that my mc's are being attahced directly onto the stage.
My normal method is to create an empty movie clip then attache my mc's to that. Is there a similar method for doing this in AS3?
Reply With Quote  
wraevn's Avatar wraevn wraevn is offline wraevn lives in United States 2008-01-23 #16 Old  
Sure -

ActionScript Code:
  1. var emptyMC:MovieClip= new MovieClip();
  2. emptyMC.name = "emptyMC";
  3. //could also use var emptyS:Sprite = new Sprite();
  4. addChild(emptyMC);
  5.  
  6. for (var countC:Number=0; countC < counCount; countC++) {
  7.     var myObject:mcCountry = new mcCountry();
  8.     emptyMC.addChild(myObject);
  9.     myObject.x=12;
  10.     myObject.y=nextcY;
  11.     myObject.txCountry.text=data_lv['country' + countC + 'country'];
  12.     myObject.txPerc.text=data_lv['country' + countC + 'countryPercent'] + " %";
  13.     myObject.txCount.text=data_lv['country' + countC + 'countryCount'];
  14.     myObject.mc.scaleX=Math.round(data_lv['country' + countC + 'countryPercent']) / 100;
  15.     nextcY+= 24;
  16. }
  17.  
  18. //then to remove
  19.  
  20. function logout():void{
  21.     removeChild(getChildByName("emptyMC"));
  22. }
Reply With Quote  
buzcajun buzcajun is offline buzcajun lives in United States 2009-06-30 #17 Old  
wraevn,

I know this is an old post, but I thought I would try it anyway. I am having some issues, removingChildren. I am doing simple load to stage an external SWF, and when I try to load another, in the same tagetMC, the other is still there and when you go back its just adds until the memory is toast.

I tried addChildAt(), I tried naming the loader, nothing seems to work.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: