2010-12-14 7 views
1

こんにちは、ありがとうございます。私はあまりにも多くの時間を苦労して過ごしました。actionscriptでローダーをアンロードする3

以下のコードは、4つの画像のスライドショーとそれらの画像のサムネイルを読み込みます。それはうまく動作します。

「invis_button」というボタンを追加しました。これは、各ローダーのremoveChildコマンドを使用して、スライドショーを構成する3つのローダーを削除することになっています。

これは問題です.3人のローダーがスライドショーに参加しています。 removeChildコマンドは、ローダーの1つ(「loader3」という名前)を削除しますが、他の2つ(「container3」および「thumbLoader3」)は削除しません。 "未定義プロパティthumbLoader3"または "Container3"のアクセスを示すエラーを返します。

誰かが私にこの理由を教えてもらえますか?または、より良い方法として、そのボタン(invis_button)をスライドショー全体をアンロードする方法。これは、あなたが見ているものの背後にある全体の理由がある...しかし、スタータのため、「thumbloader3」と「container3は」あなたが完了したら意味loadThumbs3()メソッドにローカルにスコープされている場合

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

わかりません関数を実行すると、それらのオブジェクトに対するFlashのハンドルは失われます(全く異なるスコープであることは言うまでもなく)...それらの2つのクラスレベルのプロパティを作成してみてください。それが完了したら、後でそれらをステージから正常に削除できるはずです。

あなたのオブジェクトも適切に破壊されていることを願っています。簡潔にするために、上記のコードを省略することにしました。

上記のコードを編集しました&プロパティを適切なスコープに入れました。 (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

+0

あなたの提供したコードの修正版を上記の私の投稿に編集しました – greatdecay

関連する問題