0

I've a container called mc, inside of him I generate a grid of movieclips in order to make a wall of options. When I select one of this option, this message appears:

Error #2025: The supplied DisplayObject must be a child of the caller..

The code is:


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

        }

    }


René Pflamm
  • 3,273
  • 17
  • 29
m4g4bu
  • 457
  • 2
  • 7
  • 19

2 Answers2

0

It's not clear from the code above what the object "grilla" actually is referring to.

If you are trying to remove grilla, you could just write:

grilla.parent.removeChild(grilla);

I'm not sure if that's what you're trying to do. If you're trying to remove the object you just clicked, try:

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

Update: Ok, I see what you are trying to do now.

You'll want to use the keyword "this" to refer to the current instance of "grilla".

Try this:

this.parent.removeChild(this);
shackleton
  • 701
  • 1
  • 12
  • 27
  • I try with the first code and nothing change: Line 75 1120: Access of undefined property grilla. – m4g4bu Feb 24 '12 at 05:01
  • you're going to have to supply more information. post the rest of the code that contains the object grilla – shackleton Feb 24 '12 at 05:03
0

Try parent.removeChild(this);

Although it's not the most clean way to do it. You'd be better off dispatching an event that iniciarApp will listen to.

Senekis
  • 111
  • 8
  • You mean that I've to dispatch an event when I create the grilla ( addEventListener(iniciarApp.removeChild(this), detectingEvent); and the following code in grilla.as? private function detectingEvent(e:Event):void { } or What would you suggest? – m4g4bu Feb 24 '12 at 13:44
  • When you create grilla, you'll do something like grilla.addEventListener(SomeEvent.STRING,removeGrilla); removeGrilla being a method that will remove it from the display list and remove its listener. Then on the instance of Grilla, you'll do dispatchEvent(new SomeEvent(SomeEvent.STRING)); – Senekis Feb 24 '12 at 22:43