1
votes

I wrote a AS3 class to make a Counter function. Click and hold mouse in blue area to define a value. I try to show a indicator picture to users that reminding the variation.

But when I drag mouse a little quick in the blue area a error will occur:

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

I have read some similar issue posts, but I can't fix this. Could you give me any help? Thank you!

Download .fla and .as in CS6

Download .fla and .as in CS5

The code is below:

package  {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.MouseEvent;
    import flash.text.TextFormat;
    import flash.events.Event;

    public class test extends Sprite{
        var i:int=20; //Set a var for the number displayed in stage.
        var mx1:Number; //Set a var to save MouseY when MOUSE_DOWN
        var mx2:Number; //Set a var to save MouseY when MOUSE_UP
        var num:int=0; //Set a var to calculate result
        var sub1:subbar1=new subbar1();
        var sub2:subbar2=new subbar2();
        var sub3:subbar3=new subbar3();
        var add1:addbar1=new addbar1();
        var add2:addbar2=new addbar2();
        var add3:addbar3=new addbar3();

        public function test() {
            init1(); //Set TextField and addEventListener
            initbar(); //Set indicator picture position when drag mouse
        }

        private function init1():void{
            label=new TextField();
            label.text=String(i);
            label.width=280;
            label.selectable=false;
            label.x=140;
            label.y=90;
            addChild(label);
            Controler.addEventListener(MouseEvent.MOUSE_DOWN,get1); //addEventListener to bluearea
        }

        private function initbar(){
            sub1.x=sub2.x=sub3.x=add1.x=add2.x=add3.x=30;
            sub1.y=35;
            sub2.y=55;
            add3.y=sub3.y=75;
            add2.y=95;
            add1.y=115;
        }

        private function get1(evt:MouseEvent):void{
            mx1=mouseY;
            trace(mx1);
            Controler.removeEventListener(MouseEvent.MOUSE_DOWN,get1);
            stage.addEventListener(MouseEvent.MOUSE_UP,get2); //addEventListener to MOUSE_UP
            stage.addEventListener(Event.ENTER_FRAME,lifebar); //add ENTER_FRAME to display indicator picture when move mouse
        }

        private function get2(evt:MouseEvent):void{
            mx2=mouseY;
            trace(mx2);
            if(mx2<=135&&mx2>=35&&mouseX<=130&&mouseX>=50){ //Limited enable area as the blue area
                if(num>=4){ //Set i value depends on num
                    i=i-3;
                }else if(num<=-4){
                    i=i+3;
                }else{
                    i=i-num;
                }
                label.text=String(i);
            }
            if(num==1){ //remove indicator picture when MOUSE_UP
                removeChild(sub1);
            }
            if(num==2){
                removeChild(sub1);
                removeChild(sub2);
            }
            if(num>=3){
                removeChild(sub1);
                removeChild(sub2);
                removeChild(sub3);
            }
            if(num==-1){
                removeChild(add1);
            }
            if(num==-2){
                removeChild(add1);
                removeChild(add2);
            }
            if(num<=-3){
                removeChild(add1);
                removeChild(add2);
                removeChild(add3);
            }
            stage.removeEventListener(MouseEvent.MOUSE_UP,get2);
            Controler.addEventListener(MouseEvent.MOUSE_DOWN,get1);
            stage.removeEventListener(Event.ENTER_FRAME,lifebar);
        }

        private function lifebar(evt:Event):void{ //Set a ENTER_FRAME to display indicator picture
            num=(mouseY-mx1)/12+1;
            trace(num);
            if(mouseY!=mx1&&num==1){
                addChild(sub1);

            }
            if(num==2){
                addChild(sub2);
            }
            if(num==3){
                addChild(sub3);
            }
            if(num==-1){
                addChild(add1);
            }
            if(num==-2){
                addChild(add2);
            }
            if(num==-3){
                addChild(add3);
            }

        }

    }

}
1
sounds like you are trying to remove something thats already removed, try checking with yourContainer.contains(yourChild) first.Neil
Hi Neil, yes, I tried to remove something. This error will happened when I drag mouse quickly, it will be fine if I drag mouse slowly. I don't know the reason and how to fix it actually.Nick
I used this function to make the drag operation: get1-MOUSE_DOWN lifebar-moving mouse get2-MOUSE_UPNick
Can you save your fla for CS5 and provide a link?net.uk.sweet
You should seriously work on your naming "convention". With names like sub1, sub2, sub3, get1, get2, etc. NOBODY will be willing to dive into your code very deep to look for problems.TheSHEEEP

1 Answers

1
votes

You are attempting to remove display objects that have not yet been added as a child to the display list.

Even though sub1 has been instantiated, it has not been added at the time you attempt to remove it:

child

Before calling removeChild(obj) on the display object, first test if it has been added as a child by evaluating whether if(contains(obj)) is true.

At line 67 in test.as, you should perform condition testing to see if sub1 has been added to the display list:

if(num==1) {
    if(contains(sub1)) // test to see if sub1 is on the display list
        removeChild(sub1);
}

If this issue continues with other children, you should add additional testing in blocks like these:

if(num==2){
    removeChild(sub1);
    removeChild(sub2);
}