如何创建AS3缓存图像的循环?

时间:2015-08-27 16:11:40

标签: actionscript-3 flash

我在舞台上有100个动画片段,名为ads_box_1 ... ads_box_100。每个ads_box中有另一个动画片段,名为photo_box。我想要缓存100张图片(1.jpg,2.jpg,...,100.jpg )从服务器并将它们添加到每个ads_box.photo_box.I尝试一些循环来做到这一点,但他们没有工作。所以解决方案是什么?这是我的代码:

import org.sgmnt.lib.net.*;
import flash.net.URLRequest;
import flash.filesystem.File;
for (var i:Number=1; i<=5; i++)
{
    this["ads_box_" + i].photo_box.alpha = 0;
}
LocalCacheSettings.WORKING_DIRECTORY = File.applicationStorageDirectory;
//how to create a loop frome here...
var pic_loader:Loader;
NetClassFactory.initialize( LocalCacheLoader, LocalCacheURLLoader, LocalCacheNetStream );
pic_loader = NetClassFactory.createLoader();
pic_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, _onComplete );
var pic_string:String = "http://localhost/Pics/" + String(1) + ".jpg";
pic_loader.load( new URLRequest(pic_string));
function _onComplete(e:Event):void
{
    var new_pic_mc:Sprite= new Sprite();
    new_pic_mc.addChild(pic_loader);
    new_pic_mc.width = new_pic_mc.height = 90;
    this["ads_box_" + 1].photo_box.addChild(new_pic_mc);
    this["ads_box_" + 1].photo_box.alpha = 1;
}
//to here

2 个答案:

答案 0 :(得分:0)

执行类似的操作,请注意,这只是按顺序循环加载图像。您还应该添加事件侦听器以进行错误处理。您必须在此基础上编写缓存逻辑:

import org.sgmnt.lib.net.*;
import flash.net.URLRequest;
import flash.filesystem.File;
for (var i:Number=1; i<=5; i++)
{
    this["ads_box_" + i].photo_box.alpha = 0;
}
LocalCacheSettings.WORKING_DIRECTORY = File.applicationStorageDirectory;
//how to create a loop frome here...
var pic_loader:Loader;
NetClassFactory.initialize( LocalCacheLoader, LocalCacheURLLoader, LocalCacheNetStream );
pic_loader = NetClassFactory.createLoader();
pic_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, _onComplete );
var currentImageCount:uint = 1;
//load the first image
loadImage();

function loadImage():void
{
    var pic_string:String = "http://localhost/Pics/" + String(currentImageCount) + ".jpg";
    pic_loader.load( new URLRequest(pic_string));
}

function _onComplete(e:Event):void
{
    var new_pic_mc:Sprite= new Sprite();
    new_pic_mc.addChild(pic_loader);
    new_pic_mc.width = new_pic_mc.height = 90;
    this["ads_box_" + currentImageCount].photo_box.addChild(new_pic_mc);
    this["ads_box_" + currentImageCount].photo_box.alpha = 1;
    //WRITE YOUR CACHING LOGIC HERE FOR EACH LOADED IMAGE
    if (currentImageCount <= 100)
    {
        currentImageCount++;
        loadImage();
    }
    else
    {
        pic_loader.contentLoaderInfo.removeEventListener( Event.COMPLETE, _onComplete);
    }
}

希望这能回答你的问题。

答案 1 :(得分:0)

最后这段代码很好用,谢谢@Gurtej Singh

import org.sgmnt.lib.net.*;
import flash.net.URLRequest;
import flash.filesystem.File;
for (var i:Number=1; i<=5; i++)
{
    this["ads_box_" + i].photo_box.alpha = 0;
}
LocalCacheSettings.WORKING_DIRECTORY = File.applicationStorageDirectory;
var currentImageCount:uint = 1;
var pic_loader:Loader;
loadImage();
function loadImage():void
{
    NetClassFactory.initialize( LocalCacheLoader, LocalCacheURLLoader, LocalCacheNetStream );
    pic_loader = NetClassFactory.createLoader();
    pic_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, _onComplete );
    var pic_string:String = "http://localhost/Pics/" + String(currentImageCount) + ".jpg";
    pic_loader.load( new URLRequest(pic_string));
}

function _onComplete(e:Event):void
{
    var new_pic_mc:Sprite= new Sprite();
    new_pic_mc.addChild(pic_loader);
    new_pic_mc.width = new_pic_mc.height = 90;
    this["ads_box_" + currentImageCount].photo_box.addChild(new_pic_mc);
    this["ads_box_" + currentImageCount].photo_box.alpha = 1;
    pic_loader.contentLoaderInfo.removeEventListener( Event.COMPLETE, _onComplete );
    if (currentImageCount < 5)
    {
        currentImageCount++;
        loadImage();
    }
    else
    {
        pic_loader.contentLoaderInfo.removeEventListener( Event.COMPLETE, _onComplete);
    }
}