从XML获取随机数据使用AS3与无重复

从XML获取随机数据使用AS3与无重复

本文介绍了从XML获取随机数据使用AS3与无重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是从使用ActionScript 3.0不带重复的previous场地的XML文件加载挣扎在随机数据。

I am still struggling loading in random data from an XML file using Actionscript 3.0 with no repeat of the previous venue.

基本上,当我点击舞台上的按钮,我想一个随机地点从我的XML在舞台上动态文本框来显示。我的 XML 就设在这里 http://pastebin.com/0qMMkfCs

Basically when I click a button on stage I want a random venue from my xml to display in dynamic text boxes on stage. My XML is located here http://pastebin.com/0qMMkfCs

倒不如直接访问数据,而无需创建一个数组!

It would be better to access the data directly without creating an array!

当然,有人知道如何正确地做到这一点。三江源:)

Surely someone knows how to do this correctly. Thankyou :)

推荐答案

确定,此编辑将介绍如何确保他们都来了一次随机:

OK, this edit will show how to ensure they all come up once randomly:

private var xmlLoader:URLLoader = new URLLoader();
private var myLoadedXML:XML;
private var venues:XMLList;

public function getAnotherRandomVenue():XML{
    if(venues.length() > 0){
        var random:Number = Math.floor(Math.random() * (venues.length()-1));
        var thisRandomVenue:XML = new XML(venues[random]);
        delete venues[random];
        return thisRandomVenue;
    }
    return null;
}

public function loadItUP():void{
    xmlLoader.addEventListener(Event.COMPLETE, onXMLLoaded);
    xmlLoader.load(new URLRequest("http://path.to.venuesXML.xml"));
}

private function onXMLLoaded(e:Event):void {
    myLoadedXML = new XML(e.target.data);
    venues = myLoadedXML.venue;
    // you are now ready at this point to start calling getAnotherRandomVenue()
}

现在呼吁getAnotherRandomVenue会回报你一个地点XML对象。现在,您可以任何一个)重复调用这个方法直到返回null(所有项目已随机返回)或b)看venues.length()调用它。

Now calling getAnotherRandomVenue will return you a single Venue XML object. You can now either a) call this method repeatedly until it returns null (all items have been randomly returned) or b) look at venues.length() before you call it.

编辑:合并了code清楚,当事情得到加载,并呼吁

merged the code to clearly when things get loaded and called.

这篇关于从XML获取随机数据使用AS3与无重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 14:18