這是我該如何去做。我沒有做的唯一事情是在影片加載後改變影片剪輯的位置。您可以輕鬆地在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