xml - 将图像从 XML、Flash、Actionscript 3.0 加载到单独的影片剪辑中

标签 xml flash actionscript-3

我有一个 xml 图像库,非常标准,我有一个加载器,以及我想要将图像加载到其中的影片剪辑,我遇到的问题是我想要将图像加载到单独的影片剪辑中,所以我使用案例陈述来指定他们去哪里。但是,我只能让它们加载到单个影片剪辑中,我假设它们是相互加载的,我不知道如何让它们分开。我会发布我的代码。这对我来说没有任何意义,但如果你有任何建议,那就太好了。

我可以制作单独的加载器,然后每个加载器只做 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;    
       }
   }

最佳答案

下面是我将如何处理它。我唯一没有做的是在加载图像后更改 MovieClip 的位置。您可以在 imageLoaded() 函数中轻松添加此功能。

将以下代码放在 AS3 FLA 文件第一帧的“ Action ”面板上。这假设名为“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 文档中的文件名填充数组。

关于xml - 将图像从 XML、Flash、Actionscript 3.0 加载到单独的影片剪辑中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2990010/

相关文章:

android - scrollView 中的 fillViewPort 到底是做什么的?

xml - Xpath - 根据前 sibling 和后 sibling 选择节点

javascript - xsd 类型 javascript 的限制

javascript - 如何将现有播客嵌入到我的网站中?

actionscript-3 - 优化 AS3 SWF 文件的大小?

actionscript-3 - NetStream.info,出现错误 #2154

apache-flex - 是否可以在 Actionscript 3 中在运行时向对象添加成员变量?

没有命名空间定义的 XML 命名空间前缀

ruby-on-rails - 在 Rails 上扩展 Flash 消息的功能?自动隐藏、嵌入链接 ?关闭按钮?

xml - ActionScript - 将数据分配给对象数组后处理 XML?