将图像加载到XML,Flash,Actionscript 3.0中的单独影片剪辑中

时间:2010-06-07 13:45:26

标签: xml flash actionscript-3

我有一个非常标准的xml图像库,我有一个加载器,以及我想要加载图像的影片剪辑,我遇到的问题是我想将图像加载到单独的影片剪辑中,所以我使用case语句来指定它们的去向。但是,我只能将它们加载到一个影片剪辑中,我认为它们正在加载彼此的顶部,我不知道如何将它们分开。我会发布我的代码。这对我没有任何意义,但如果你有任何建议真的很棒。

我可以制作单独的加载器,然后每个加载器只做1个图像,但这对我来说听起来不对。

   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).xml.photos.imageOne.image.@url[0]));
                whole.boxOne.pictureLoader.addChild(photoLoader);
                trace("1Done");
                break;

            case 2:
                photoLoader.load(new URLRequest(MovieClip(this.parent).xml.photos.imageTwo.image.@url[0]));
                whole.boxTwo.pictureLoader.addChild(photoLoader);
                trace("2Done");
                break;    
       }
   }

1 个答案:

答案 0 :(得分:1)

以下是我将如何进行的。我唯一没做的就是在加载图像后改变MovieClip的位置。您可以在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文档的AS3

//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);
}

当然,您的XML文档可能非常不同,在这种情况下,您需要练习检索所需的确切信息。在我看来,这是最棘手的部分。此外,您还希望实例化上述loadXML函数范围之外的数组。然后,使用XML文档中的文件名递归填充数组。