ArgumentError:removeChild时出现错误#2025

时间:2012-07-11 07:57:21

标签: actionscript-3 flash actionscript

我写了一个AS3类来制作一个Counter函数。在蓝色区域中单击并按住鼠标以定义值。我尝试向用户显示提示变化的指示图片。

但是当我在蓝色区域快速拖动鼠标时会出现错误:

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

我已经阅读了一些类似的帖子,但我无法解决这个问题。你能帮我一下吗?谢谢!

Download .fla and .as in CS6

Download .fla and .as in CS5

代码如下:

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 个答案:

答案 0 :(得分:1)

您正尝试将尚未作为子项添加的显示对象移除到显示列表中。

即使已经实例化了sub1,但在您尝试删除它时尚未添加它:

child

在显示对象上调用removeChild(obj)之前,首先通过评估if(contains(obj))是否为真来测试它是否已作为子项添加。

在test.as的第67行,您应该执行条件测试以查看是否已将sub1添加到显示列表中:

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

如果其他孩子继续这个问题,你应该在这些块中添加额外的测试:

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