2010-12-14 145 views
1

您好,非常感謝您的關注。我花了很多時間掙扎。使用actionscript 3卸載加載程序

下面的代碼加載了四張圖片的幻燈片,以及這些圖片的縮略圖。它工作正常。

我已經添加了一個名爲「invis_button」的按鈕,當按下它時,應該刪除組成幻燈片的3個加載程序,對每個加載程序使用removeChild命令。

但這就是問題所在,幻燈片放映中涉及到3個加載器。 removeChild命令成功移除了其中一個加載器(名爲「loader3」),但未成功移除其他兩個(「container3」和「thumbLoader3」)。它返回一個錯誤,指出「訪問未定義的屬性thumbLoader3」或「Container3」。

有人可以告訴我這是爲什麼嗎?或者更好的是,如何讓該按鈕(invis_button)卸載整個幻燈片。

var images3:Array = ["ad_bona1.jpg", "ad_bona2.jpg", "ad_darkhawk1.jpg", "ad_darkhawk2.jpg"]; 

var thumbX3:Number = -375; 
var thumbY3:Number = 220; 

var loader3:Loader = new Loader(); 
loader3.load(new URLRequest("assets/ad_bona1.jpg")); 
addChild(loader3); 
loader3.alpha = 0; 

loadThumbs3(); 

function loadThumbs3():void 
{ 
var thumbLoader3:Loader; 
var container3:Sprite = new Sprite(); 
addChild(container3); 
container3.buttonMode = true; 
for(var i3:uint = 0; i3 < images3.length; i3++) 
{ 
thumbLoader3 = new Loader(); 
thumbLoader3.load(new URLRequest("assets/thumbs/" + images3[i3])); 
thumbLoader3.x = thumbX3; 
thumbLoader3.y = thumbY3; 
thumbX3 += 85; 
container3.addChild(thumbLoader3); 
thumbLoader3.addEventListener(MouseEvent.CLICK, thumbClicked3); 
} 
} 
function thumbClicked3(event:MouseEvent):void 
{ 
var path3:String = event.currentTarget.contentLoaderInfo.url; 
path3 = path3.substr(path3.lastIndexOf("/") + 1); 
loader3.load(new URLRequest("assets/" + path3)); 
} 


///PROBLEM BELOW, button removes only "loader3" and not the other two for some reason 

invis_button.addEventListener(MouseEvent.CLICK, unload_loaders); 

function unload_loaders(event:MouseEvent):void{ 
    removeChild(loader3); 
    removeChild(thumbLoader3); 
    removeChild(container3); 
} 

回答

2

不知道這背後是你觀察一下整個原因......但是對於初學者來說,「thumbloader3」和「container3」的本地範圍的loadThumbs3()方法,這意味着一旦你完成執行這個函數,那些Flash對這些對象的句柄就會丟失(更不用說在一個完全不同的範圍內)......嘗試爲這兩個類創建類級屬性。一旦完成,你應該能夠在以後成功地將它們從舞臺上移除。

我希望你也正確地銷燬你的對象,爲了簡潔起見,你只是選擇忽略上面的代碼。

我編輯了上面的代碼&將屬性放入適當的範圍。 (thumbLoader3的多個副本現在被收集到一個向量(專用陣列)中,以便它們可以在需要時予以妥善處理以消除它們)

我也給你寫了一個適當的銷燬方法。 ;)

我還沒有在我自己的機器上試過,但給它一個旋轉&看看它是怎麼回事。

var images3:Array = ["ad_bona1.jpg", "ad_bona2.jpg", "ad_darkhawk1.jpg", "ad_darkhawk2.jpg"]; 

var thumbX3:Number = -375; 
var thumbY3:Number = 220; 

// begin new instance properties.. 
// created a new property, allowing you to group (and hold on to) the multiple thumbLoaders 
var thumbLoader3Vector:Vector.<Loader> = new Vector.<Loader>(); 
var container3:Sprite; 
// end new instance properties 

var loader3:Loader = new Loader(); 

loader3.load(new URLRequest("assets/ad_bona1.jpg")); 
addChild(loader3); 
loader3.alpha = 0; 

loadThumbs3(); 

function loadThumbs3():void 
{ 

    // this is where container3 used to be declared 

    container3 = new Sprite(); 
    addChild(container3); 
    container3.buttonMode = true; 
    for(var i3:uint = 0; i3 < images3.length; i3++) 
    { 
     var tPtr:int = thumbLoader3Vector.length; 
     thumbLoader3Vector.push(new Loader()); 
     // this is where thumbLoader3 used to be declared & instantiated 

     thumbLoader3Vector[tPtr].load(new URLRequest("assets/thumbs/" + images3[i3])); 
     thumbLoader3Vector[tPtr].x = thumbX3; 
     thumbLoader3Vector[tPtr].y = thumbY3; 
     thumbX3 += 85; 
     container3.addChild(thumbLoader3Vector[tPtr]); 
     thumbLoader3Vector[tPtr].addEventListener(MouseEvent.CLICK, thumbClicked3); 

    } 
} 
function thumbClicked3(event:MouseEvent):void 
{ 
    var path3:String = event.currentTarget.contentLoaderInfo.url; 
    path3 = path3.substr(path3.lastIndexOf("/") + 1); 
    loader3.load(new URLRequest("assets/" + path3)); 
} 


///PROBLEM BELOW, button removes only "loader3" and not the other two for some reason 

invis_button.addEventListener(MouseEvent.CLICK, unload_loaders); 

function unload_loaders(event:MouseEvent):void{ 

    // since the thumbLoader3 Loaders are children of container3 in the display list, we need to remove them first 
    for(var $i:uint = 0;$i<thumbLoader3Vector.length;$i++) 
    { 
     removeChild(thumbLoader3Vector[$i]); 
     // also make sure you remove the listener, so that the object will be picked up by garbage collection 
     thumbLoader3Vector[$i].removeEventListener(MouseEvent.CLICK, thumbClicked3); 
    } 
    // and then just set the entire vector to null 
    thumbLoader3Vector = null; 

    // remove the loader3 object & set it to null 
    removeChild(loader3); 
    loader3 = null; 

    // remove the container3 object & set it to null 
    removeChild(container3); 
    container3 = null; 
} 
+0

不能告訴你我多麼感激迴應。我對as3相當陌生,有沒有可用於爲這些對象創建類級屬性的代碼? – Steve 2010-12-14 21:23:22

+0

編輯我的帖子上面,包括您提供的代碼的修改版本 – greatdecay 2010-12-14 21:47:32