使用actionscript 3卸载Loader

时间:2010-12-14 20:21:27

标签: actionscript-3 slideshow loader

您好,非常感谢您对此的看法。我花了太多时间挣扎。

下面的代码加载了四张图片的幻灯片,以及这些图片的缩略图。它工作正常。

我添加了一个名为“invis_button”的按钮,当按下该按钮时,应该使用每个加载器的removeChild命令删除构成幻灯片的3个加载器。

但这就是问题,幻灯片放映中涉及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);
 }

1 个答案:

答案 0 :(得分:2)

不确定这是否是你正在观察的背后的全部原因...但是对于初学者来说,“thumbloader3”和“container3”本地作用于loadThumbs3()方法,这意味着一旦你完成了该功能, Flash对这些对象的句柄丢失了(更不用说在完全不同的范围内)...尝试为这两个对象创建类级属性。完成后,您应该能够在以后成功地从舞台上删除它们。

我希望你也正确地破坏了你的对象,为了简洁起见,你只是选择省略上面的代码。

我已编辑了您上面的代码&amp;将属性放入适当的范围。 (thumbLoader3的多个副本现在被收集在一个向量(专用数组)中,以便在销毁它们时可以正确地寻址它们。)

我还给你写了一个合适的破坏方法。 ;)

我没有在我自己的机器上试过它,但给它一个旋转&amp;看看它是怎么回事。

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