Skip to main content
Participating Frequently
May 19, 2010
Question

remove child

  • May 19, 2010
  • 1 reply
  • 945 views

i want to delete object which i early created using addchild.
here is  the code i try to do that

function popup(event:MouseEvent):void
{
      var _pentagen_02:pentagen = new pentagen();
      addChild(_pentagen_02);
      _pentagen_02.x = 350;
      _pentagen_02.y =  200;
      _pentagen_02.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
      _pentagen_02.addEventListener(MouseEvent.MOUSE_WHEEL, size);
      _pentagen_02.addEventListener(MouseEvent.MOUSE_UP, dropIt);
      _pentagen_02.buttonMode = true;

}

function  delobject():void
{
if (endX < _imageX )
      {
      removeChild(_pentagen_02);
      }
}

i also try to using "   removeChild(this);  " but this also not work.

This topic has been closed for replies.

1 reply

thenaaAuthor
Participating Frequently
May 19, 2010
i correct that (create a global variable) .
but  i have another problem regarding to that .
there is a button which i  click it generate _pentagen_02 more than one(any number of instances)  then it become the problem to remove.
as a example when i click that  button twice it create two pentagans. now i try to remove those pentagen  ,first one  remove ok second one show me following output

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


this is the code  which i correct. can any one help?.....................

var  _pentagen_02:pentagen;

function popup(event:MouseEvent):void
{
_pentagen_02 = new pentagen();
addChild(_pentagen_02); 
}

function delobject():void
{
if (endX <  _imageX )
{
removeChild( _pentagen_02);
}
}
Ned Murphy
Legend
May 19, 2010

Each time you reassign _pentagen_02 it changes which object it is referencing.  When you remove the object it is referencing it does not automatically switch back to one of the porevious objects it used to be.

Try using an array to store all of the instances you create.  Then you can remove them by targeting them in the array.  The only thing being... when you use removeChild, you are not deleting the object, you are removing it from view.  To delete the object you must null it out after removal.

See if the following helps...

var  _pentagen_02:pentagen;

var _pentArray:Array = new Array();

function popup(event:MouseEvent):void
{
_pentagen_02 = new pentagen();
addChild(_pentagen_02);

_pentArray.push(_pentagen_02);
}

function delobject():void
{
if (endX <  _imageX )
{

_pentagen_02 = _pentArray.pop();
removeChild( _pentagen_02);

_pentagen_02 = null;
}
}

thenaaAuthor
Participating Frequently
May 20, 2010

thank lot both of you.  i also try in that  way but it is not worked becuase of following line

_pentagen_02 = _pentArray.pop();

i did not include above line to my action script. can you explain what is that line.

google search i found following line

removeChildAt(2);

it also worked  without changing anything(no need to add  array).

not removeChild(1) or removeChildAt(3).