2010-06-07 40 views
3

我有一個xml圖像庫,非常標準,並且我有一個加載程序以及我想要加載圖像的影片剪輯,跑到是我想要圖像加載到單獨的影片剪輯,所以我使用case語句來指定他們去哪裏。不過,我只能讓它們加載到一個影片剪輯中,我假設它們正在加載ontop,我不知道如何讓它們分離出來。我會發布我的代碼。這對我來說沒有任何意義,但如果你有任何建議,那將是非常棒的。將圖像從XML,Flash,Actionscript 3.0加載到單獨的影片剪輯中

我可以製作單獨的裝載機,然後每個裝載程序只做一個圖像,但是這聽起來並不正確。

var counterNumber:Number = 0; 

    function callThumbs():void{ 
    for (var i:Number = 0; i <3; i++){ 
     thumbLoaded(); 
     counterNumber++; 
    } 
    } 




    function thumbLoaded(){ 

    var photoLoader = new Loader(); 

    switch (counterNumber){ 

      case 1: 
       photoLoader.load(new URLRequest(MovieClip(this.parent)[email protected][0])); 
       whole.boxOne.pictureLoader.addChild(photoLoader); 
       trace("1Done"); 
       break; 

      case 2: 
       photoLoader.load(new URLRequest(MovieClip(this.parent)[email protected][0])); 
       whole.boxTwo.pictureLoader.addChild(photoLoader); 
       trace("2Done"); 
       break;  
     } 
    } 

回答

1

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

+0

我期待您的回覆,謝謝您的關注。 – 2010-06-07 16:38:07

+0

快速思考你在這裏給了我什麼,謝謝你btw。假設我想從XML文件中獲取圖像,而不是將它們包含到/ images文件夾中並以這種方式進行定位,那可能嗎?通過也許組織圖像到他們自己的數組? – 2010-06-08 01:04:28

+0

查看關於加載我剛添加的XML的附加信息。 – 2010-06-08 02:54:49

相關問題