Image AS3中的异步图像加载
我知道映像将在AS3中异步加载,并且应该使用事件和事件侦听器来处理同步 所以,在一个简单的例子中,它看起来是这样的:Image AS3中的异步图像加载,image,actionscript-3,asynchronous,Image,Actionscript 3,Asynchronous,我知道映像将在AS3中异步加载,并且应该使用事件和事件侦听器来处理同步 所以,在一个简单的例子中,它看起来是这样的: var loader : Loader = new Loader(); var im_file: URLRequest = new URLRequest ("imfile.png"); loader.load(im_file); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loading_complete);
var loader : Loader = new Loader();
var im_file: URLRequest = new URLRequest ("imfile.png");
loader.load(im_file);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loading_complete);
function loading_complete (e : Event) : void
{ // ... do smt with your loaded data // }
function loadNext():void
{
if(queue.length > 0)
{
// Notice here that we use .pop() on the queue, which will select and
// remove the last item from queue.
var req:URLRequest = new URLRequest( queue.pop() );
var photo:Loader = new Loader();
photo.load(req);
photo.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
}
else
{
// The queue is finished - dispatch an event or whatever you fancy to
// let the rest of the application know we're done here.
trace("Queue finished.");
}
}
我想做的是有一个预加载类,它将加载我事先需要的所有图像。
在这种情况下,如何让所有其他类知道加载何时完成
我是否发送事件?在这种情况下,最好的做法是什么
提前感谢,
Praskaton您很可能希望创建一个队列并将图像路径添加到队列中。然后,在完成每个图像的加载后,继续处理队列中的下一个项目。加载所有图像后,您会发送一个完整的事件或类似的事件,让您的应用程序知道一切都已完成
检查或以了解它们是如何实现单个或批量映像加载的。您很可能希望创建一个队列并将映像路径添加到队列中。然后,在完成每个图像的加载后,继续处理队列中的下一个项目。加载所有图像后,您会发送一个完整的事件或类似的事件,让您的应用程序知道一切都已完成
检查或了解它们是如何实现单个或批量映像加载的。除了@Boon提供的答案之外,您还可以通过以下方式进行映像队列的实际设置 首先,您需要一个列表来存储仍然需要加载的所有图像。这使您可以轻松地定义任意多个图像。它可以是“队列”:
var queue:Array = [
"http://interfacelift.com/wallpaper/previews/03177_orionnebulaintheinfrared@2x.jpg",
"http://interfacelift.com/wallpaper/previews/03175_purpleclouds@2x.jpg",
"http://interfacelift.com/wallpaper/previews/03173_goodmorning2013@2x.jpg"
];
下一步要做的是建立我们正在做的事情的“核心”方法。它将处理加载下一个图像以及在队列为空时通知我们。它看起来像这样:
var loader : Loader = new Loader();
var im_file: URLRequest = new URLRequest ("imfile.png");
loader.load(im_file);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loading_complete);
function loading_complete (e : Event) : void
{ // ... do smt with your loaded data // }
function loadNext():void
{
if(queue.length > 0)
{
// Notice here that we use .pop() on the queue, which will select and
// remove the last item from queue.
var req:URLRequest = new URLRequest( queue.pop() );
var photo:Loader = new Loader();
photo.load(req);
photo.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
}
else
{
// The queue is finished - dispatch an event or whatever you fancy to
// let the rest of the application know we're done here.
trace("Queue finished.");
}
}
当然,我们的侦听器功能是处理加载图像的完成。请注意,这里我们调用loadNext()
——这是在当前加载映像完成后才开始加载队列中的下一个映像的关键
function loadComplete(e:Event):void
{
addChild(e.target.content as Bitmap);
// Begin loading next image in the queue.
loadNext();
}
当然,为了启动这个过程,我们只需要使用它,如果队列为空,它会立即通知我们队列已完成,或者开始按顺序加载图像
// Start loading the queue.
loadNext();
附加/整理: 如果您希望能够回收这些代码或只是整理一下,您可以很容易地将其放入一个类中。该类可以被称为
ImageQueue
,其结构将包含上述queue
数组、loadNext()
方法和loadComplete()
方法。它还可以有一个add()
方法,用于最初以更干净的方式将图像添加到队列中
下面是该类的基础,如果你感兴趣的话,你可以完成它:
public class ImageQueue
{
private var _queue:Array = [];
public function add(image:String):void{ }
public function loadNext():void{ }
private function _loadComplete(e:Event):void{ }
}
除了@Boon提供的答案之外,您还可以这样着手实际设置映像队列 首先,您需要一个列表来存储仍然需要加载的所有图像。这使您可以轻松地定义任意多个图像。它可以是“队列”:
var queue:Array = [
"http://interfacelift.com/wallpaper/previews/03177_orionnebulaintheinfrared@2x.jpg",
"http://interfacelift.com/wallpaper/previews/03175_purpleclouds@2x.jpg",
"http://interfacelift.com/wallpaper/previews/03173_goodmorning2013@2x.jpg"
];
下一步要做的是建立我们正在做的事情的“核心”方法。它将处理加载下一个图像以及在队列为空时通知我们。它看起来像这样:
var loader : Loader = new Loader();
var im_file: URLRequest = new URLRequest ("imfile.png");
loader.load(im_file);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loading_complete);
function loading_complete (e : Event) : void
{ // ... do smt with your loaded data // }
function loadNext():void
{
if(queue.length > 0)
{
// Notice here that we use .pop() on the queue, which will select and
// remove the last item from queue.
var req:URLRequest = new URLRequest( queue.pop() );
var photo:Loader = new Loader();
photo.load(req);
photo.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
}
else
{
// The queue is finished - dispatch an event or whatever you fancy to
// let the rest of the application know we're done here.
trace("Queue finished.");
}
}
当然,我们的侦听器功能是处理加载图像的完成。请注意,这里我们调用loadNext()
——这是在当前加载映像完成后才开始加载队列中的下一个映像的关键
function loadComplete(e:Event):void
{
addChild(e.target.content as Bitmap);
// Begin loading next image in the queue.
loadNext();
}
当然,为了启动这个过程,我们只需要使用它,如果队列为空,它会立即通知我们队列已完成,或者开始按顺序加载图像
// Start loading the queue.
loadNext();
附加/整理: 如果您希望能够回收这些代码或只是整理一下,您可以很容易地将其放入一个类中。该类可以被称为
ImageQueue
,其结构将包含上述queue
数组、loadNext()
方法和loadComplete()
方法。它还可以有一个add()
方法,用于最初以更干净的方式将图像添加到队列中
下面是该类的基础,如果你感兴趣的话,你可以完成它:
public class ImageQueue
{
private var _queue:Array = [];
public function add(image:String):void{ }
public function loadNext():void{ }
private function _loadComplete(e:Event):void{ }
}
或者,格林斯托克
s
LoaderMax`和BulkLoader
也能很好地处理这个问题。或者,格林斯托克s
LoaderMax`和BulkLoader
也能很好地处理这个问题。