2010-06-07 38 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

相关问题