开发者

Unloading a Loader with actionscript 3

Hello and thank you very much for looking at this. I've spent too many hours struggling.

The code below loads a slideshow of four images, along with thumbnails for those images. It works fine.

I've added a button called "invis_button", that when pressed is supposed to remove the 3 loaders that make up the slideshow, using the removeChild command for each loader.

But this is the problem, there are 3 loaders involved in the slide-show. The removeChild command successfully removes one of the loaders (named "loader3"), but not the other two ("container3", and "thumbLoader3"). It returns an error stating "access of undefined property thumbLoader3" or "Container3".

Can someone tell me why this is ? Or better still, how to make that button (invis_button) unload the entire slide-show.

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);
 }


Not sure if this is the entire reason behind what you're observing... but for starters, "thumbloader3" and "container3" are scoped locally to the loadThumbs3() method, which means once you finish executing the function, Flash's handles to those objects are lost (not to mention being in an entirely different scope)... try creating class-level properties for those two. Once that's done you should be able to successfully remove them from the stage later on.

I hope that you're also properly destroying your objects, and for the sake of brevity you just chose to omit that code above.

I've edited the code you had above & put the properties into the proper scope. (the multiple copies of thumbLoader3 are now collected inside of a vector (specialized array) so that they can be properly addressed when it comes time to destroy them)

I also wrote you a proper destroy method. ;)

I haven't tried it on my own machine, but give it a spin & see how it goes.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜