Actionscript 3 在图像加载程序中创建的位图的空值
我有一个函数,可以从库中获取图像,并在加载后在舞台上呈现它们Actionscript 3 在图像加载程序中创建的位图的空值,actionscript-3,Actionscript 3,我有一个函数,可以从库中获取图像,并在加载后在舞台上呈现它们 function addStuff(){ this["otherstuff" + index] = new Textfield; addChild(this["otherstuff" + index]); var IMAGE_URL:String = arraywithstuff[index][2]; var ldr:Loader = new Loader(); ldr.contentLoad
function addStuff(){
this["otherstuff" + index] = new Textfield;
addChild(this["otherstuff" + index]);
var IMAGE_URL:String = arraywithstuff[index][2];
var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, ldr_complete);
ldr.load(new URLRequest(IMAGE_URL));
function ldr_complete(evt:Event):void {
var bmp:Bitmap = ldr.content as Bitmap;
this["photo" + index] = new Bitmap(bmp.bitmapData);
addchild(this["photo" + index]);
然后,我稍后将尝试在另一个函数中删除照片
function removeStuff(){
for (var i = 0; i<maxindex; i++){
removeChild(this["otherstuff" + i]);
removeChild(this["photo" + i]);
}
函数removeStuff(){
对于(var i=0;iIt)您可能会在addStuff调用后更改索引,因此当您稍后调用removeStuff时,“photo”+索引值已经不同了。我不这么认为。我删除了for循环中的一堆内容,所有索引都不同,但它会在第一张照片处停止,这是photo0。我包含了更多代码。现在它会在第一张照片中停止。如果我删除removeChild(this[“photo”+I]);
行,它会删除所有其他内容。跟踪“photo”ldr_complete方法中的+索引。我认为所有照片都被分配到相同的索引值(即maxindex)。是的,我尝试了此方法,但它们确实跟踪为photo0、photo1、photo2等。在addStuff调用后,您可能会更改索引,因此当您稍后调用removeStuff时,“photo”+索引值已经不同了。我不这么认为。我删除了for循环中的一堆内容,所有索引都不同,但它会在第一张照片处停止,这是photo0。我包含了更多代码。现在它会在第一张照片中停止。如果我删除removeChild(this[“photo”+I]);
行,它会删除所有其他内容。跟踪“photo”+ldr_complete方法内的索引。我认为您的所有照片都被分配到相同的索引值(即maxindex).是的,我试过了,但它们确实像photo0、photo1、photo2这样跟踪。@MagnusMyrgård我没有检查,但理论上它应该工作,至少在移除部分。@MagnusMyrgård我没有检查,但理论上它应该工作,至少在移除部分。
// Create the array.
var aList:Array = new Array;
function addStuff()
{
// ...
// Your code.
var aLoader:Loader = new Loader;
aLoader:Loader.contentLoaderInfo.addEventListener(Event.INIT, onBitmap);
aLoader:Loader.load(new URLRequest(IMAGE_URL));
// Your code.
// ...
}
// Don't declare methods inside other methods.
function onBitmap(e:Event):void
{
var anInfo:ContentLoaderInfo = e.target;
var aRaster:Bitmap = anInfo.content as Bitmap;
// Unsubscribe the listener.
anInfo.removeEventListener(Event.INIT, onBitmap);
// Store the reference.
aList.push(aRaster);
addChild(aRaster);
}
function deleteStuff():void
{
while (aList.length)
{
var aRaster:Bitmap = aList.pop();
removeChild(aRaster);
// Release the loaded data.
aRaster.bitmapData.dispose();
}
}