好吧,到目前爲止沒有任何內容被從內存中刪除,這實際上取決於如何引用movie1。比方說,稍後在應用程序中想要再次添加movie1,您應該會發現movie1不僅在這裏,而且還包含movie2 & movie3。
var movie1:MovieClip;
whatever();
anotherMethod();
function whatever():void
{
movie1 = new MovieClip();
var movie2:MovieClip = new MovieClip();
var movie3:MovieClip = new MovieClip();
movie1.addChild(movie2);
movie1.addChild(movie3);
addChild(movie1);
whateverElse();
}
function whateverElse():void
{
//here, nothing is garbage collected, movie1 exists outside
//the scope of this function
removeChild(movie1);
//now you can
tryThis();
//but if you do this you're effectively killing movie2 & movie3 , since
//they're not referenced anywhere else.
removeChild(movie1);
movie1 = null;
//this is going to throw an error , movie1's gone!
tryThis();
}
function tryThis():void
{
//yes ,it's still here with all its content
addChild(movie1);
}
function anotherMethod():void
{
var movieN:MovieClip = new MovieClip();
var movie2:MovieClip = new MovieClip();
var movie3:MovieClip = new MovieClip();
movieN.addChild(movie2);
movieN.addChild(movie3);
// do all you need to do...
// then
removeChild(movieN);
getOut();
}
function getOut():void
{
//life goes on without movieN, movie2 & movie3
//waiting to be garbage collected
}
所以答案是肯定的,如果我想從內存中完全刪除它,我必須刪除所有的子元素,然後它是父元素,以保持應用程序的緊密度 – 2010-09-18 00:27:40
如果movie2&movie3未被任何其他對象引用,則刪除movie1就足夠了。看代碼!在另一個方法()我只刪除movieN&在getOut()movieN不再被引用,所以三個movieclip將被垃圾收集 – PatrickS 2010-09-18 02:01:34