AS3如何在外部图像加载在一个循环? [英] AS3 how to load in external images in a loop?

查看:137
本文介绍了AS3如何在外部图像加载在一个循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个高分表在Flash中使用AS3,我已经成功地得到了工作code,显示需要显示的用户国国旗的姓名和成绩同时也是我的高评分表的一部分。为标志的图像被存储在远程服务器上。

现在我知道如何加载在一个单一的图像,并将其添加到我的影片剪辑,但事情就当我想通过一个循环迭代20多个加载速度很复杂。我已经看了很多例子,只是不能采取抽样code为我工作。反正没有进一步的一个在这里做的是我有这么远。<​​/ P>

 //将服务器高分
VAR hiscoreloader:的URLLoader =新的URLLoader();
hiscoreloader.addEventListener(引发Event.COMPLETE,highScoresLoaded);
VAR hiscorexml:XML;
功能highScoresLoaded(五:事件):无效
{
    hiscorexml =新的XML(e.target.data);
    VAR hsList:返回XMLList = hiscorexml.highscores.highscore;
    对于(VAR我:UINT = 0; I&LT; hsList.length();我++)
    {
        这[hs_score _+ I]的.text = ZEROPAD(hsList.score.text()[I],8);
        这种[hs_name _+ i]的.text = hsList.name.text()[我]
        //装载的标志
        // URL标记将是:http://wfwxg.philosophydesign.com/images/flags/+ hsList.country.text()[我] +巴纽
        //将剪辑实例命名上面的图片需求增加也将是:这个[hs_flag _+ I]
    }
}
hiscoreloader.load(新的URLRequest(http://wfwxg.philosophydesign.com/includes/top20.php?nocache=+新的日期()的getTime())); 

任何人都可以修改我的code做我后?

非常感谢

斯科特

解决方案

这里有一个快速的解决方案。

实际上,我会建议封装的担忧,使您的code是不是太紧密耦合。现在,你处理几乎一切都在一个单一的功能,同时将结果分配数据到主影片剪辑。这就是为什么我创建了一个数组来保存XML数据,以及加载的图像。

一个可能的结构可以是:
- 首先检索XML数据,并将其分配给数据数组
。 - 环通的数据阵列来加载图像,并指定加载的内容向相关对象
。 - 所有的图像加载,使用数据阵列来设置显示

  //将服务器高分
        VAR hiscoreloader:的URLLoader =新的URLLoader();

            //创建一个数组来保存XML数据
        VAR数据:数组= [];

        hiscoreloader.addEventListener(引发Event.COMPLETE,highScoresLoaded);

        VAR hiscorexml:XML;

        功能highScoresLoaded(五:事件):无效
        {
          hiscorexml =新的XML(e.target.data);
              VAR hsList:返回XMLList = hiscorexml.highscores.highscore;

          对于(VAR我:UINT = 0; I&LT; hsList.length();我++)
          {
             //这里我们创建一个新的对象来保存检索到的数据
             //并把它添加到数据阵列
            数据[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()[我] +png格式,
             mc_name:hs_flag _+ i.toString();
            };

             //加载图像
             VAR装载机:装载机=新的Loader();
             configureListeners(loader.contentLoaderInfo);
             loader.name = i.toString();
             loader.load(新的URLRequest(数据[I] .flagURL));
          }
        }

        功能configureListeners(信息:的LoaderInfo):无效
        {
            //在此处添加所有的事件监听器
            info.addEventListener(引发Event.COMPLETE,在completeHandler);
        }

        功能removeListeners(信息:的LoaderInfo):无效
        {
            //在此处删除所有的事件监听器
            info.removeEventListener(引发Event.COMPLETE,在completeHandler);
        }


    功能在completeHandler(事件:事件):无效
    {
        VAR指数:INT = INT(event.currentTarget.loader.name);
        //图像数据添加到您的数据阵列
        数据[指数] .flag = event.currentTarget.loader.content;
        removeListeners(event.currentTarget);
    }
 

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. 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. 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. This is why I created an Array to hold the XML data as well as the loaded images.

One possible structure could be to:
- first retrieve the XML data and assign it to the data Array.
- loop thru the data Array to load the images , and assign the loaded content to the relevant 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 );
    }

这篇关于AS3如何在外部图像加载在一个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆