views:

63

answers:

2

I have a movieclip (childMc) that is the child of another movieclip (parentMc) on the stage. My code creates a reference (refMc) to the child movieclip on the stage, and then deletes the previous parent movieclip (parentMc) through a call to function deleteChild. The problem is that the delete is deleting the reference also. How do I break the reference so that the reference (refMc) is kept on the stage? Here is my code:

Stage Code:

var refMc:MovieClip;
var parentMc:MovieClip=this.addChild(new parentSymbol()); // parentSymbol has childMc inside of it
function deleteChild(e:Event)
  {
  refMc=parentMc.childMc;
  this.removeChild(parentMc);
  }

Any help would be appreciated.

+1  A: 

Well, removing the parent will remove all its chidren from stage. There are two ways to handle this situation

1--If you want to delete parent but not its child then remove the Child from Parent Container first and add it to stage or another MovieClip like this

var parent2:MovieClip= new MovieClip(); function deleteChild(e:Event) {

refMc=parentMc.childMc;

addChild(refMc); // this will add refMC to stage

OR

parent2.addChild(refMc); // this will add to another movieClip

this.removeChild(parentMc);

}

And your function deleteChild is just removing the Children from stage not from memory. Try addChild(parentMc) after your deleteChild function is called, you will get both the container OR addChild(refMc) will bring the Child to stage.. Means reference stays in memory so your movie-clips are in memory but not on stage. so your reference are not deleted by removing the Movie-clips unless they are set to NULL

Muhammad Irfan
+1  A: 

Since parentMc is defined outside the deleteChild function, parentMc is not deleted, it's only removed from the stage, therefore childMc should still be accessible.

In any case, following your code example, you could do something like this:

var refMc:MovieClip;
var container:MovieClip = new MovieClip();

// parentSymbol has childMc inside of it
var parentMc:MovieClip = this.addChild(new parentSymbol()); 

function deleteChild(e:Event)
{
  refMc = parentMc.childMc;
  container.addChild(refMc);

  this.removeChild(parentMc);
}
PatrickS