这是我该如何去做。我没有做的唯一事情是在影片加载后改变影片剪辑的位置。您可以轻松地在imageLoaded()函数中添加此功能。
将以下代码放在AS3 FLA文件第一帧的“动作”面板上。这假定名为'images'的目录与FLA/SWF存在于相同的目录中。
//Create array of image filenames
var filenames:Array = new Array();
filenames[0] = 'some_image.jpg';
filenames[1] = 'some_other_image.jpg';
filenames[2] = 'and_another.jpg';
//Declare variables
const IMAGES_DIRECTORY:String = 'images/';
var loaders:Array = new Array();
var movieClips:Array = new Array();
//This function instantiates the exact number of Loader objects that are required
//and initiates the loading process for each image file. As each image is loaded,
//imageLoaded() is called
function createLoaders(filenamesAsArray:Array, directory:String):void
{
for(var i:int = 0; i < filenamesAsArray.length; i++)
{
loaders[i] = new Loader();
loaders[i].load(new URLRequest(directory + filenamesAsArray[i]));
loaders[i].contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
}
}
createLoaders(filenames, IMAGES_DIRECTORY);
//This function is called as each Loader object is completely loaded
//It creates each individual movieclip, adds the image to it, and then adds the MC to the stage
function imageLoaded(e:Event):void
{
if(e.target.content.bitmapData)
{
var mc:MovieClip = new MovieClip();
var bmp:Bitmap = new Bitmap(e.target.content.bitmapData);
mc.addChild(bmp);
movieClips.push(mc);
addChild(movieClips[movieClips.length - 1]);
}
}
关于导入XML数据
查找到XML Object。
加载XML示例
的实施例的XML文档:
<images>
<image>
<title>Portrait of a Woman</title>
<filename>portrait.jpg</filename>
</image>
<image>
<title>Rural Landscape</title>
<filename>landscape.jpg</filename>
</image>
<image>
<title>My Cat</title>
<filename>cat.jpg</filename>
</image>
</images>
用于装载上述XML文档
//Instantiate some objects (URLoader and XML)
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
//Listen for the instance of URLLoader (xmlLoader) to trigger Event.COMPLETE,
//which will call the function named 'loadXML()'
xmlLoader.addEventListener(Event.COMPLETE, loadXML);
//Initiate the process of loading the XML document
//In this case, 'test.xml' is located in the same directory as the SWF/FLA
//that this code is located in
xmlLoader.load(new URLRequest('test.xml'));
//This is the function that will be called by the event listener above (when
//the xmlLoader signals Event.COMPLETE
function loadXML(e:Event):void
{
//Retrieve the contents of the XML document
xmlData = new XML(e.target.data);
//Trace the entire document:
trace(xmlData);
//Trace the filename for the first <image> node
trace(xmlData.image[0].filename);
}
当然AS3,XML文档可以是非常不同,在这种情况下,您需要练习检索您想要的确切信息。在我看来,这是最棘手的部分。另外,你会想实例化一个在上面的loadXML
函数范围之外的数组。然后,递归地使用XML文档中的文件名填充数组。
我期待您的回复,谢谢您的关注。 – 2010-06-07 16:38:07
快速思考你在这里给了我什么,谢谢你btw。假设我想从XML文件中获取图像,而不是将它们包含到/ images文件夹中并以这种方式进行定位,那可能吗?通过也许组织图像到他们自己的数组? – 2010-06-08 01:04:28
查看关于加载我刚添加的XML的附加信息。 – 2010-06-08 02:54:49