9 Replies Latest reply on Apr 28, 2010 8:35 AM by kamlam4

    About RemoveChild()

    kamlam4

      I met a problem Error#1009 when I want to remove some movieclips from the stage

       

      for(var g:Number = 0; g<10; g++){
                  var ClassRef_RG:Class = Class(getDefinitionByName(classNames_G[Math.floor(Math.random()*classNames_G.length)]));
                  var classInstance_RG:* = new ClassRef_RG();
                  removeChild(getChildByName(classInstance_RG));
      }     

       

       

      can someone help?

        • 1. Re: About RemoveChild()
          Ned Murphy Adobe Community Professional & MVP

          If you didn't add the child, you cannot remove the child

           

          var classInstance_RG:* = new ClassRef_RG();

           

          addChild(classInstance_RG);


          removeChild(getChildByName(classInstance_RG));

           

          Also, classInstance_RG is not the name of the object, it is a reference to it.  You should only need to use...

           

          removeChild(classInstance_RG);

          • 2. Re: About RemoveChild()
            kamlam4 Level 1

            oh sorry, it is only part of my codes

            this is the codes about adding fish and remove fish

             

            I got 2 problems here:

            1) when I add in small fish, no small fish is seen but the counter is right

            2)an error # 1009 is shown and no removechild is done

             

             

            //----- storing the number of fish -----//
            var num_S:Number = 0;
            var num_B:Number = 0;
            var num_G:Number = 0;
            var num_P:Number = 0;

             


            var classNames_S:Array = ["smallfish1", "smallfish2", "smallfish3", "smallfish4", "smallfish5", "smallfish6", "smallfish7", "smallfish8", "smallfish9"];
            var classNames_B:Array = ["bigfish1", "bigfish2", "bigfish3", "bigfish4"];
            var classNames_G:Array = ["green1"];
            var classNames_P:Array = ["pollutant1"];
            var Oname:Array = ["s","b","c","p"];

             


            //----- adding small fish -----//
            function addSmallFish():void{
                var ClassRef_S:Class = Class(getDefinitionByName(classNames_S[Math.floor(Math.random()*classNames_S.length)]));/ /choose a class
                var classInstance_S:* = new ClassRef_S();
                addChild(classInstance_S);
                num_S++;
                trace("Small Fish =" + num_S);
                checkEco(num_S,Oname[0]);
            }

             


            //----- adding big fish -----//
            function addBigFish():void{
                //var classNames_B:Array = ["bigfish", "smallfish"];
                var ClassRef_B:Class = Class(getDefinitionByName(classNames_B[Math.floor(Math.random()*classNames_B.length)]));
                var classInstance_B:* = new ClassRef_B();
                addChild(classInstance_B);
                num_B++;
                trace("Big Fish =" + num_B);
                checkEco(num_B,Oname[1]);
            }

             


            //----- adding coral -----//
            function addGreen():void{
                //var classNames_G:Array = ["bigfish", "smallfish"];
                var ClassRef_G:Class = Class(getDefinitionByName(classNames_G[Math.floor(Math.random()*classNames_G.length)]));
                var classInstance_G:* = new ClassRef_G();
                addChild(classInstance_G);
                num_G++;
                trace("Coral =" + num_G);
                checkEco(num_G, Oname[2]);
            }

             


            //----- adding pollutant -----//
            function addPoll():void{
                //var classNames_P:Array = ["bigfish", "smallfish"];
                var ClassRef_P:Class = Class(getDefinitionByName(classNames_P[Math.floor(Math.random()*classNames_P.length)]));
                var classInstance_P:* = new ClassRef_P();
                addChild(classInstance_P);
                num_P++;
                trace("Pollutant =" + num_P)
                checkEco(num_P, Oname[3]);
               
            }

             

            function checkEco(num_o:Number, name_o:String):void{
                var remove_s:Number = 0;
                var remove_b:Number = 0;
                var remove_c:Number = 0;
                var add_b:Number = 0;
                var add_s:Number = 0;
                var pollutane_re:Number = 0;
                trace("In Eco");
               
                if(name_o == "p"){ // pollutants poisoned marine life
                    if(num_o != 0){
                        pollutane_re = num_o;
                        remove_s = num_o*8;
                        remove_b = num_o*2;
                        remove_c = num_o*4;
                        RemovalOrAdd(remove_s, remove_b, remove_c,add_b,add_s,pollutane_re);
                        num_P = 0; // pollutants reduced to 0
                    }
                }
                if(name_o == "b"){ // big fish ate small fish
                    if(num_o != 0){
                        remove_s = num_o*5;
                        RemovalOrAdd(remove_s, remove_b, remove_c,add_b,add_s,pollutane_re);
                    }
                }
                if(name_o == "s"){ // small fish as food for big fish
                    if(num_o > 20){
                        add_b = Math.floor(num_o/20);
                        RemovalOrAdd(remove_s, remove_b, remove_c,add_b,add_s,pollutane_re);
                    }
                }
                if(name_o == "c"){ // coral provided habitat for small fish
                    if(num_o > 10){
                        add_s = Math.floor(num_o/5);
                        RemovalOrAdd(remove_s, remove_b, remove_c,add_b,add_s,pollutane_re);
                    }
                }   
            }

             

            function RemovalOrAdd(r1:Number,r2:Number, r3:Number, r4:Number, r5:Number, r6:Number):void{
                trace("In Remov");
                if(r1 != 0){
                    for(var s:Number = 0; s<r1; s++){
                        var ClassRef_RS:Class = Class(getDefinitionByName(classNames_S[Math.floor(Math.random()*classNames_S.length)]));
                        var classInstance_RS:* = new ClassRef_RS();
                        removeChild(classInstance_RS);
                        num_S--;
                        trace("Small Fish reduce to " + num_S)
                    }
                }
                if(r2 != 0){
                    for(var b:Number = 0; b<r2; b++){
                        var ClassRef_RB:Class = Class(getDefinitionByName(classNames_B[Math.floor(Math.random()*classNames_B.length)]));
                        var classInstance_RB:* = new ClassRef_RB();
                        removeChild(classInstance_RB);
                        num_B--;
                        trace("Big Fish reduce to " + num_B)
                    }
                }
                if(r3 != 0){
                    for(var g:Number = 0; g<r3; g++){
                        var ClassRef_RG:Class = Class(getDefinitionByName(classNames_G[Math.floor(Math.random()*classNames_G.length)]));
                        var classInstance_RG:* = new ClassRef_RG();
                        removeChild(classInstance_RG);
                        num_G--;
                        trace("Coral reduce to " + num_G)
                    }       
                }   
                if(r4 != 0){
                    for(var h:Number = 0; h<r4; h++){
                        addBigFish();
                    }
                }
                if(r5 != 0){
                    for(var f:Number = 0; f<r5; f++){
                        addSmallFish();
                    }
                }
                if(r6 != 0){
                    for(var j:Number = 0; j<r6; j++){
                        removeChild(getChildByName("pollutant1"));
                    }
                }
            }

             


            // ----- End of program ----- //

            • 3. Re: About RemoveChild()
              Ned Murphy Adobe Community Professional & MVP

              My answer doesn't change.  In your remove functions you are defining brand new objects but are not adding them.

               

              ...

              var classInstance_RS:* = new ClassRef_RS(); // declare a new instance
              removeChild(classInstance_RS);  // remove that same unadded instance?!

              ...

              That code is not targeting any object you have added to the stage previously.  It is failing to add new ones and then failing to remove the ones that weren't added

               

              If you want to remove objects that were added earlier, your best bet may be to store references to them in an array as you add them,  Then you can target them directly to remove them.

              • 4. Re: About RemoveChild()
                kamlam4 Level 1

                oh i see mu problem...

                however, can you tell me how to store reference? I have never come across this/ \"

                • 5. Re: About RemoveChild()
                  Ned Murphy Adobe Community Professional & MVP

                  var smallFishArray:Array = new Array();

                   

                  function addSmallFish():void{
                      var ClassRef_S:Class = Class(getDefinitionByName(classNames_S[Math.floor(Math.random()*classNames_S.le ngth)]));//choose a class
                      var classInstance_S:* = new ClassRef_S();

                      smallFishArray.push(classInstance_S);
                      addChild(classInstance_S);
                      num_S++;
                      trace("Small Fish =" + num_S);
                      checkEco(num_S,Oname[0]);
                  }

                  • 6. Re: About RemoveChild()
                    kamlam4 Level 1

                    oh my god... this is quite confused me, but really thank you very much!!!

                     

                    so, if I want to remove child

                     

                    I can say removeChild(smallFishArray[0])?

                     

                    but what should I do after I remove the child with the array?

                    maybe say like this

                    if I remove the movieclip : smallFishArray[0] smallFishArray[1] and smallFishArray[2]

                    after I have removed them, will the first 3 positions in the smallFishArray left blank ?

                    • 7. Re: About RemoveChild()
                      Ned Murphy Adobe Community Professional & MVP

                      No, by removing the instances named in the array you do not change the contents of the array.  What you should also do is set value of the array to null at that point followed by removing the element from the array.  You should look at the methods that are supported by the Array class so that you get an idea of what you need to manipulate it.

                      • 8. Re: About RemoveChild()
                        kamlam4 Level 1

                        I do a little try, is it like this? using b1 to store the one that I am going to remove

                         

                         

                        stage.addEventListener(MouseEvent.CLICK, addA1);
                        stage.addEventListener(KeyboardEvent.KEY_DOWN, removeA1);

                         

                        var a1Array:Array = new Array();

                         

                        function addA1(evt:MouseEvent):void{
                            var a1:MovieClip = new A1();
                            a1Array.push(a1);
                            a1.x = Math.random()*stage.stageWidth;
                            a1.y = Math.random()*stage.stageHeight;
                            addChild(a1);
                        }

                         

                        function removeA1(evt:KeyboardEvent):void{
                            var b1:DisplayObject = a1Array.pop();
                            removeChild(b1);
                        }

                        • 9. Re: About RemoveChild()
                          Ned Murphy Adobe Community Professional & MVP

                          I don't know if your way will work properly but it seems it might.  I would just remove the child first, then assign a null value to the array element, then remove it from the array.

                           

                          function removeA1(evt:KeyboardEvent):void{
                              removeChild(a1Array[a1Array.length-1]); // removes the last object named in the array

                               a1Array[a1Array.length-1] = null;  // nullifies the reference to the object

                               a1Array.pop(); // remove the reference from the array
                          }