简体   繁体   English

AS3如何循环加载外部图像?

[英]AS3 how to load in external images in a loop?

I'm making a highscore table in flash using AS3, I have successfully got working code that displays the names and the scores but also part of my high score table it needs to display the users country flag. 我正在使用AS3在Flash中制作一个高分表,我已经成功获得了可以显示名称和分数的工作代码,但它还是显示用户国家/地区标志所需的高分表的一部分。 The images for the flags are stored on a remote server. 标志的图像存储在远程服务器上。

Now I know how to load in a single image and add it to my movie clip but things get very complicated when I want to load in 20+ by iterating through a loop. 现在,我知道如何加载单个图像并将其添加到我的影片剪辑中,但是当我想通过循环迭代加载20+时,事情变得非常复杂。 I've looked at many examples and just cant adopt the sample code to work for me. 我看了很多例子,只是不能采用示例代码为我工作。 Anyway without a further a do here is what I have so far. 无论如何没有进一步做到这里是我到目前为止。

// Load High scores from server
var hiscoreloader:URLLoader = new URLLoader();
hiscoreloader.addEventListener(Event.COMPLETE, highScoresLoaded);
var hiscorexml:XML;
function highScoresLoaded(e:Event):void
{
    hiscorexml = new XML(e.target.data);
    var hsList:XMLList = hiscorexml.highscores.highscore;
    for(var i:uint = 0; i < hsList.length(); i++)
    {
        this["hs_score_"+i].text = zeroPad(hsList.score.text()[i], 8);
        this["hs_name_"+i].text = hsList.name.text()[i];
        // Load in Flag
        // url of flag will be: "http://wfwxg.philosophydesign.com/images/flags/" + hsList.country.text()[i] + ".png"
        // Move clip instance name the above image needs adding too will be: this["hs_flag_"+i]
    }
}
hiscoreloader.load(new URLRequest("http://wfwxg.philosophydesign.com/includes/top20.php?nocache=" + new Date().getTime()));

Can anyone amend my code to do what I'm after? 任何人都可以修改我的代码以执行我要执行的操作吗?

Many Thanks 非常感谢

Scott 斯科特

Here's a quick solution. 这是一个快速的解决方案。

Practically , I would advise to encapsulate concerns so that your code is not too tightly coupled. 实际上,我建议封装关注点,以使您的代码不会过于紧密地耦合在一起。 Right now, you handle almost everything in one single function while assigning the result data to your main MovieClip. 现在,在将结果数据分配给主MovieClip时,您可以在一个函数中处理几乎所有内容。 This is why I created an Array to hold the XML data as well as the loaded images. 这就是为什么我创建了一个Array来保存XML数据以及加载的图像。

One possible structure could be to: 一种可能的结构可能是:
- first retrieve the XML data and assign it to the data Array. - 首先检索XML数据并将其分配给数据阵列。
- loop thru the data Array to load the images , and assign the loaded content to the relevant Object. -遍历数据数组以加载图像,并将加载的内容分配给相关的Object。
- After all the images have loaded , use the data Array to set your display - 加载完所有图像后,使用数据阵列设置显示

       // Load High scores from server
        var hiscoreloader:URLLoader = new URLLoader();

            //Create an Array to hold your XML data
        var data:Array = [];

        hiscoreloader.addEventListener(Event.COMPLETE, highScoresLoaded);

        var hiscorexml:XML;

        function highScoresLoaded(e:Event):void
        {
          hiscorexml = new XML(e.target.data);
              var hsList:XMLList = hiscorexml.highscores.highscore;

          for(var i:uint = 0; i < hsList.length(); i++)
          {
             //here we create a new Object to hold the retrieved data
             //and add it to the data Array
            data[i] = { 
                             hs_score:zeroPad(hsList.score.text()[i], 8), 
             hs_name: hsList.name.text()[i], 
             flagURL:"http://wfwxg.philosophydesign.com/images/flags/"
                             + hsList.country.text()[i] + ".png",
             mc_name:"hs_flag_"+i.toString();
            };

             //load the images
             var loader:Loader = new Loader();
             configureListeners(loader.contentLoaderInfo);  
             loader.name = i.toString();
             loader.load( new URLRequest( data[i].flagURL ) );
          }
        }

        function configureListeners(info:LoaderInfo):void
        {
            //add all your event listeners here
            info.addEventListener(Event.COMPLETE , completeHandler );
        }

        function removeListeners(info:LoaderInfo):void
        {
            //remove all your event listeners here
            info.removeEventListener(Event.COMPLETE , completeHandler );
        }


    function completeHandler(event:Event):void
    {
        var index:int = int( event.currentTarget.loader.name );
        //add the image data to your data Array
        data[index].flag = event.currentTarget.loader.content;
        removeListeners( event.currentTarget );
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM