1

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

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

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

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

在 CS6 中下载 .fla 和 .as

在 CS5 中下载 .fla 和 .as

代码如下:

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);
            }

        }

    }

}
4

1 回答 1

1

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

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

孩子

在调用显示对象之前,首先通过评估是否为真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);
}
于 2012-07-12T03:31:47.620 回答