[英]Using an array to load in multiple images in Actionscript
我有一个班级作业,必须创建一个照相馆,其中一项规定是图像必须从外部来源加载。 我们分配的参考代码为:
var myRequest:URLRequest = new URLRequest("bw1.jpg");
myLoader.load(myRequest);
addChild(myLoader);
我的特定需求要求我一次在屏幕上最多保存2张图像副本,一个作为缩略图,第二个作为全尺寸图像。 我想要这样,以便当用户单击缩略图时,将创建一个新的图片实例,并以0 alpha缩放为全尺寸,并且当新图片的alpha增加时,当前选择的图片的alpha减少。 一旦旧的图片不再可见,它将被从舞台上删除。
我在弄清楚如何在动作脚本中创建图像的副本时遇到了麻烦,而且我的for循环似乎无法正确执行,因为即使没有错误消息,它也只能执行一次迭代。
这是通过pastebin链接到我的代码的链接: http : //pastebin.com/iadgKgsk,但是为了避免您在来回之间切换,我也将其粘贴在这里
import flash.display.Bitmap;
//creates a loader for each picture to be loaded, I know I could have an empty array
that I add to but created a full array to test.
var loaders:Array = [new Loader(),new Loader(), new Loader(), new Loader(), new
Loader(), new Loader(), new Loader(), new Loader(), new Loader(), new Loader()];
//stores the url request for each image to be loaded
var Requests:Array =[new URLRequest("pic1.jpg"),new URLRequest("pic2.jpg"),new
URLRequest("pic3.jpg"),
new URLRequest("pic4.jpg"),new URLRequest("pic5.jpg"),new URLRequest("pic6.jpg"), new
URLRequest("pic7.jpg"),
new URLRequest("pic8.jpg"), new URLRequest("pic9.jpg"),new URLRequest("pic10.jpg")];
//creates 2 empty arrays one to store the thumbnail sized pics the other fullsized.
Ideally I want one Array holding the bitmap data and the other holding the thumbnail
instances since I only need 2
// fullsized images at a time I can just create a new one and erase the old one in a
single function.
var pics:Array = new Array();
var pics2:Array = new Array();
//defines an empty bitMap variable as a placeholder for which to copy from redefined in
every iteration of the loop
var test:Bitmap;
//loops through every loader
for (var i in loaders);
{
// loads the loader and url request and creates a picture
loaders[i].load(Requests[i]);
//waits for the pic to load
loaders[i].contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
//after loader is loaded create pic from the data
function loadComplete(evt:Event):void
{
var i = "Test";
trace(i);
test= evt.target.content as Bitmap;
pics2[i] = test;
pics[i] =new Bitmap(test.bitmapData);
pics2[i] =new Bitmap(test.bitmapData);
//creates an image on the stage at runtime to help debug
var pic1 = new Bitmap(test.bitmapData);
addChild(pics[i])
pic1.scaleX = 0.138427734375;
pic1.scaleY = 0.1384114583333333;
pic1.x = 204;
pic1.y = 20.6;
pic1.alpha = .25;
var pic2:Bitmap = new Bitmap(test.bitmapData);
pic2.x =100;
pic2.y =100;
pic2.scaleX =.33;
pic2.scaleY=.33;
addChild(pic2);
loaders[i].contentLoaderInfo.removeEventListener(Event.COMPLETE,
loadComplete)
}
}
for循环的设置方式是尝试对加载程序实例的每个属性进行迭代,而这并不是您想要的。 您可能还想研究其他AS3批量加载器选项,因此已经有一些脚本可以帮助您解决此问题。
您的循环应如下所示
for(var i=0; i<loaders.length; i++)
{
var curLoader = loaders[i].load(Requests[i]);
您还可以看到我放在一起的示例,该示例加载了一堆图像动画书样式的动画(使用Blender生成的图像):
http://www.shaunhusain.com/DrawTextRandomly/
http://www.shaunhusain.com/DrawTextRandomly/srcview/
在utils.imageLoader包中,我在那里创建了两个类来处理图像,因为它们是从Blender输出的,具有顺序文件名,我只是使用了一些循环来基于数字计数器加载图像,在这种情况下,您可以切换它以使用图像源数组加载。
在BatchImageLoader类中,我有一个变量,可以允许创建多少个加载器,我想查看内存与加载时间之间的性能差异(这也取决于运行时环境,但是我想了解一个总体思路,以结束最多只能使用90个装载机)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.