提供的DisplayObject必须是调用者的子级

时间:2012-02-24 04:00:26

标签: actionscript-3 removechild displayobject

我有一个名为mc的容器,在他里面我生成了一个动画片段网格,以便制作一个选项墙。当我选择其中一个选项时,会显示以下消息:

  

错误#2025:提供的DisplayObject必须是。的子级   呼叫者..

代码是:


  In the Class iniciarApp I've this:

       var mc:MovieClip = new MovieClip();
       var grilla:Grilla = new Grilla();

    mc.x = 0;
    mc.y = 0;
    mc.name = "square";
    addChild(mc);

    grilla.name = "grilla";
    grilla.x = mc.x;
    grilla.y = mc.y;
    mc.addChild(grilla);


----------


   in Grilla.as:

     public class Grilla extends MovieClip  {

        private var boxNum:int = 48;
    private var cols:int = 6;
    private var rows:int = Math.ceil(boxNum / cols);
    private var boxCount:int = 0;

    public function Grilla(){

           for (var py:int = 0; py < rows; py++) {

        for (var px:int = 0; px < cols; px++)    {

            var caja:clip = new clip();

            caja.x = -115 + caja.width * px;
            caja.y = -150 + caja.height * py;
            caja.name = "opcion" + (py + 1);
            caja.mouseChildren = false;

            var contentText = new TextField();
            var formato = new TextFormat();
            formato.size = 14;
            contentText.defaultTextFormat = formato;

            contentText.width = 36;
            contentText.height = 34;
            contentText.x = -10;
            contentText.y = -10;

            for (var u:uint = 0; u < boxNum; u++)    {
                contentText.text = "" + u;
            }

            addChild(caja);
            caja.addChild(contentText);

            if (boxCount < boxNum)   {

            caja.buttonMode = true;
            caja.addEventListener(MouseEvent.CLICK, seleccionarOpcion);

            }

            boxCount++;

             }

            }

            var barra:score = new score();
            barra.x = 80;
            barra.y = -200;
            barra.puntajeTXT.text = "hola";
            addChild(barra);

        }


        private function seleccionarOpcion(m:MouseEvent):void
        {
            TweenMax.to(MovieClip(m.target), 0.5, {scaleY: -1});
            m.target.removeEventListener(MouseEvent.CLICK, seleccionarOpcion);
            m.target.buttonMode = false;

            var opcionABuscar:String;
            opcionABuscar = m.currentTarget.name;


            var opt:String = opcionABuscar.substring(6);


     **[HERE] i need to remove the instance of grilla created in the other class**

        **m.currentTarget.parent.parent.removeChild(grilla);**  << this is not working    


    var trivia:generarTrivia = new generarTrivia(opt);

    trivia.x = 0;
    trivia.y = 0;
    trivia.name = "trivia";
    addChild(trivia);

        }

    }


2 个答案:

答案 0 :(得分:0)

从上面的代码中不清楚“grilla”实际指的是什么。

如果你想删除grilla,你可以写:

grilla.parent.removeChild(grilla);

我不确定这是不是你要做的。如果您要删除刚刚单击的对象,请尝试:

m.currentTarget.parent.removeChild(m.currentTarget);

更新:好的,我知道你现在要做的是什么。

您需要使用关键字“this”来引用“grilla”的当前实例。

试试这个:

this.parent.removeChild(this);

答案 1 :(得分:0)

尝试使用parent.removeChild(this);

虽然这不是最干净的方法。你最好调度一个iniciarApp会听的事件。

相关问题