2012-08-10 81 views
1

我已經加載了一個外部swf文件,默認播放flv文件,因爲swf被加載。現在的問題是我如何從內存中刪除swf文件。我的代碼:刪除外部swf文件as3

var myLoader:Loader = new Loader();      
var url:URLRequest = new URLRequest("ExternalSWF.swf"); 
myLoader.load(url);          
detailMovieClip.movieHolder.addChild(myLoader); 

我已經嘗試過removeChild,unload和unloadAndStop的許多組合,但都無效。我想到了所有關於不正確引用。

更新:

我去從Jegan答案,但只有當我在其中只有1 numChildren的虛擬項目在真實世界中的代碼示例numChildren的測試,howver工作報告22所以我不知道如果那會是一個問題。這裏是真正的世界代碼:

var myImageLoader:Loader; 
var myImageRequest:URLRequest; 
var theImagePath:String; 

//part from xml processor function 

theImagePath = "flash/"+myXML..item_video_link[n]; 
loadTheMovie(theImagePath); 

function loadTheMovie(theImagePath):void{ 

myImageLoader = new Loader(); 
myImageRequest= new URLRequest(theImagePath); 
myImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,showMeTheVideo); 
myImageLoader.load(myImageRequest); 

} 

function showMeTheVideo(evt:Event):void{ 

detailsMovieClip_mc.details_video_holder.dynamicVideoHolder.addChild(myImageLoader); 

} 

stopVideo(sectionname):viod{ 

if(detailsMovieClip_mc.details_video_holder.dynamicVideoHolder.numChildren !=0){ 

trace("what is the number of children: "+numChildren); 

myImageLoader.unloadAndStop(); 

detailsMovieClip_mc.details_video_holder. 
dynamicVideoHolder.removeChild(myImageLoader); 


} 
} 
+0

相信你最好的選擇是移除孩子並將其引用爲null,同時確保事件監聽器被移除,除此之外,垃圾收集器會在內存中緊張時提取它,只是在一個正常的通行證。 – shaunhusain 2012-08-10 05:22:06

回答

1
stage.addEventListener(MouseEvent.CLICK, removeSWF); 
function removeSWF (e:MouseEvent):void 
{ 
    if(detailMovieClip.movieHolder.numChildren !=0){   
     myLoader.unloadAndStop(); 

     detailMovieClip.movieHolder.removeChild(myLoader);// empty the movieClip memory 
    } 
} 

或 名稱您Loader實例,然後通過使用getChildByName

myLoader.name = "myloader"; 

function removeSWF (e:MouseEvent):void 
    { 
     if(detailMovieClip.movieHolder.numChildren !=0){   
      Loader(detailMovieClip.movieHolder.getChildByName("myloader")).unloadAndStop(); 
     detailMovieClip.movieHolder.removeChild(detailMovieClip.movieHolder.getChildByName("myloader"));// empty the movieClip memory 
     } 
    } 
+0

removeSWF函數中的mc.myLoader.unloadAndStop()是更徹底的選擇嗎? – Duke 2012-08-10 05:36:06

0

搜索我想這是因爲你要添加的裝載機趕赴現場itsef 。

要麼保持這種行爲,在這種情況下有一個快速修復,使用removeChild()從MovieClip中移除加載器,然後將引用設置爲null或使用delete關鍵字。

要麼你想正確地做到這一點,在這種情況下,監聽LOADED事件,將loader.content包含的MovieClip添加到目標MovieClip。然後,當您想卸載它時,使用removeChild(),然後使用loader.unload()從容器中刪除剪輯。