A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Help removingChild IF child has been added!

  1. #1
    Member
    Join Date
    Feb 2009
    Posts
    49

    Help removingChild IF child has been added!

    - So I have 4 functions called by their own buttons.
    Each function calls a movie clip from the library with addChild.
    - When I click on one of these 4 functions I want it to remove the added children if any of the other functions had been clicked prior.
    - Problem is, removeChild at the start of each function results in errors if a child had never been called yet. It's trying to remove something that isn't there.
    - So I tried making a 'flag:number = 0' for each of the 4 functions. Each function begins with settings its flag to '1'
    - I put a for loop at the beginning of each function saying IF any of the flags are = to 1, then remove the children.
    - I don't see why that should not work, I think the error lies in my array or my for loop if statement as I still don't fully understand them.
    So I will just post the code relating to the array and loop:

    Code:
    var flagH:Number = 0;
    var flagLi:Number = 0;
    var flagBe:Number = 0;
    var flagBr:Number = 0;
    var flagArray:Array = new Array(flagH, flagLi, flagBe, flagBr);
    
    //hydrogenOn is the first of four each one essentially doing the same thing
    function hydrogenOn(event:MouseEvent):void {
    	flagH = 1;
    	for (var i:int = 0; i<flagArray.length; i++) {
    		if (flagArray[i].Number==1) {                     //<-- this cant be right
    			table_mc.removeChild(lithium_mc);
    			table_mc.removeChild(beryllium_mc);
    			table_mc.removeChild(bromine_mc);
    		}
    	}
    table_mc.addChild(hydrogen_mc);
    // and the rest of the function.....
    btw this is the error report:

    Error #1069: Property Number not found on Number and there is no default value.
    at PhoneApp_fla::MainTimeline/hydrogenOn()

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    As the error message says, it's actually complaining about a non-existent "Number" property. This line:
    Code:
    if (flagArray[i].Number==1){
    is the culprit. There is no Number property of a Number.

    But what you really want to solve your original problem is simply the contains function. That will let you know if it's safe to call removeChild.

  3. #3
    Member
    Join Date
    Feb 2009
    Posts
    49
    yea I fixed that part forgot to update here sorry. now states:
    Code:
    if (flagArray[i]==1) {
    problem is that the swf runs with No errors yet the for loop if statement does nothing.
    What's this about a contains function?

  4. #4
    Member
    Join Date
    Feb 2009
    Posts
    49
    Ok I've now worked out my flag must be called like this

    Code:
    flagArray[0] = 1;
    and not like this
    Code:
    flagH = 1;
    but now I am given the error

    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display:: DisplayObjectContainer/removeChild()
    at PhoneApp_fla::MainTimeline/hydrogenOn()

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I tried to post about contains earlier, but I guess it didn't go through. contains is a method of DisplayObjectContainer which tells you if a DisplayObject is a child. You can use it to tell if you can safely call removeChild.

    Code:
    if (p.contains(c)){
      p.removeChild(c);
    }
    This will safely remove c if it is a child of p, and do nothing if it isn't.

  6. #6
    Member
    Join Date
    Feb 2009
    Posts
    49
    Am I able to make c = what I have below? I want it to say:
    if (p.contains(any of these children){
    p.removeChild(any of the children p contains);
    }

    So I made an array holding my classes which are 4 movieclips

    for (var i:int = 0; i<classmcArray.length; i++) {
    if (table_mc.contains(classmcArray[i])){
    table_mc.removeChild(classmcArray[i]);
    }
    }

    so I made that but get the error:
    Error #2007: Parameter child must be non-null.

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That code looks okay, but you have null somewhere in your classmcArray.

  8. #8
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    If I have a case where the object MIGHT not be on the stage, but I want it gone no matter what, I'll just wrap it in a try...catch block just to suppress the error:
    PHP Code:
    try{ removeChild(theChild); }catch($e:Error){} 
    Or if I need to use it a lot, I'll write a function that just wraps around that to keep it cleaner:
    PHP Code:
    function forceRemoveChild($parent:DisplayObjectContainer$child:DisplayObject):void
    {
        try{ 
    $parent.removeChild($child); }catch($e:Error){}
    }

    forceRemoveChild(thistheChild); 

  9. #9
    Member
    Join Date
    Feb 2009
    Posts
    49
    I'll keep that in mind as it sounds quite useful for future work. But I actually finally figured it out with the help at another forum
    got rid of all those annoying flags and arrays simply used

    PHP Code:
    var currentClip:MovieClip
    so:
    PHP Code:
    function hydrogenOn(event:MouseEvent):void {
        if(
    currentClip != null) {
        
    table_mc.removeChild(currentClip);        
        }
    }
        
    table_mc.addChild(Hydrogen_mc);
        
    currentClip Hydrogen_mc;
    //rest of function 
    works like a charm!

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