Actionscript 3 Flash AS3:根据图像高度从循环中定位加载的图像
我试图动态地堆叠通过xml文件拉入的图像。下面是我正在做的,它几乎起作用了。问题在于,它似乎只在最后一个事件上触发事件完成函数,而不是针对所有事件。有没有一种方法可以让它为每个图像运行偶数.complete函数Actionscript 3 Flash AS3:根据图像高度从循环中定位加载的图像,actionscript-3,loops,load,addeventlistener,Actionscript 3,Loops,Load,Addeventlistener,我试图动态地堆叠通过xml文件拉入的图像。下面是我正在做的,它几乎起作用了。问题在于,它似乎只在最后一个事件上触发事件完成函数,而不是针对所有事件。有没有一种方法可以让它为每个图像运行偶数.complete函数 function aboutfileLoaded(event:Event):void { aboutXML = new XML(aboutTextLoader.data); for(var l:int = 0; l < aboutXML.aboutimages.image.
function aboutfileLoaded(event:Event):void {
aboutXML = new XML(aboutTextLoader.data);
for(var l:int = 0; l < aboutXML.aboutimages.image.length(); l++)
{
imageLoader = new Loader();
imageSource = aboutXML.aboutimages.image[l];
if (imageSource != "") {
this.imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, aboutimageLoaded);
this.imageLoader.load(new URLRequest(imageSource));
//aboutBox.aboutContent.addChild(imageLoader);
//imageLoader.y = imageYpos;
//imageYpos = imageYpos + 50;
}
}
}
function aboutimageLoaded(event:Event):void {
aboutBox.aboutContent.addChild(imageLoader);
this.imageLoader.y = imageYpos;
imageYpos = imageYpos + this.imageLoader.height;
}
函数aboutfileLoaded(事件:事件):void{
aboutXML=新XML(aboutTextLoader.data);
for(var l:int=0;l
我使用我编写的一个简单图像类加载所有图像
public function loadImages(xml:XML):void {
for each(var img:XML in xml.images) {
var i:Image = new Image(img.src, img.width, img.height);
i.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, positionImage);
}
}
/**/
private function positionImage(e:Event):void {
var i:Image = e.target;
gallery.addChild(i);
// Do the positioning.
}
在上面的代码中,您可以随时更改aboutimageLoaded函数:
// In aboutfileLoaded() change all "this.imageLoader" to just "imageLoader"
function aboutimageLoader(event:Event):void {
aboutBox.aboutContent.addChild(event.target);
event.target.y = imageYpos;
}
每个加载程序一次只能处理一个作业,因此您可以将作业堆叠起来,以便逐个加载:
//Global vars
loadIndex:int = 0;
imageLoader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, aboutimageLoaded);
loadImage();
private function loadimage(){
if(aboutXML.aboutimages.image[loadIndex] != null){
imageSource = aboutXML.aboutimages.image[loadIndex];
if (imageSource != "") {
imageLoader.load(new URLRequest(imageSource));
}
}
}
function aboutimageLoaded(event:Event):void {
var holder = (ev.content as Bitmap).clone();
aboutBox.aboutContent.addChild(holder);
holder .y = imageYpos;
imageYpos = imageYpos + holder.height;
loadIndex ++;
loadimage();
}
或创建多个加载程序实例,每个实例一个:
for(var l:int = 0; l < aboutXML.aboutimages.image.length(); l++)
{
var imageLoaderTemp = new Loader();
imageSource = aboutXML.aboutimages.image[l];
if (imageSource != "") {
imageLoaderTemp.contentLoaderInfo.addEventListener(Event.COMPLETE, aboutimageLoaded);
imageLoaderTemp.load(new URLRequest(imageSource));
}
}
}
function aboutimageLoaded(event:Event):void {
aboutBox.aboutContent.addChild(event.target);
event.target.y = imageYpos;
imageYpos = imageYpos + event.target.height;
}
for(var l:int=0;l
您的第二个选项对我来说似乎很理想,但不起作用。我得到“访问未定义属性ev”。或者,如果我将其更改为event,我得到addChild的“可能使用静态类型对象隐式强制值…”。啊,对不起,“ev”应该替换为“event”,编辑以更正我找到它。需要是event.target.content。将内容更改为event.target会导致错误“将具有静态类型对象的值隐式强制转换为可能不相关的类型flash:DisplayObject“source”aboutBox.aboutContent.addChild(event.target);“哦,这只是出于演示目的而在这里做的。我通常只是在“gallery.addChild(e.target)”中使用e.target,就像我上面所说的,使用event.target会给我这个错误,如果我注释掉addChild行,我会得到.y和position.ah的错误,我发现了它。需要是event.target.content。