AS3重复功能

时间:2014-03-10 09:37:01

标签: xml actionscript-3 flash function duplicates

所以我对这段代码玩的很少,并尝试复制它,以便flashtip2的动态文本将显示来自xml的不同值,如:return(Math.floor(Math.random()*(0 + 0 + 0)) + 13); - 关于动态文本flashtip2。如何在没有重复值错误的情况下复制它?我的代码是:

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("http://www.cbbh.ba/kursna_bs.xml"));

/* This loads the XML */
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
parseData(xmlData);
 }

 /* This gets the data for today's tip */
 function parseData(mTip:XML):void {

     var itemXMLList:XMLList = XMLList(mTip..item);
     var count:int = itemXMLList.length();
     var finalcount:int = count - 1;
     //trace(finalcount);

 function randomRange(minNum:Number, maxNum:Number):Number 
 {
return (Math.floor(Math.random() * (0 + 0 + 0)) + 12);  //+ 12 or 13 
 }
 var randomNum = randomRange(0, finalcount);
 trace(randomRange(11, 11));

 flashTip.htmlText = mTip.channel.item[randomNum].description.text();

 }

1 个答案:

答案 0 :(得分:0)

按货币获取节点

要获得所需货币的确切节点,您可以按货币代码进行搜索。如果在列表中找不到货币,则函数将返回null;

function getNodeByCurrency(currency:String, currencyData:XML):XML {
    var items:XMLList = currencyData..item;
    var i:uint, len:uint = items.length();

    for (i; i < len; ++i) {
        if (String(items[i].title).indexOf(currency) != -1) {
            return items[i];
        }
    }

    //Currency isn't found
    return null;
}

用法:

trace(getNodeByCurrency("USD", xmlData));
trace(getNodeByCurrency("CAD", xmlData).description);

从预定义范围获取随机值。

如果你想要范围内的随机值,你应该稍微改变你的功能:

function randomValueFromRange(minValue:int, maxValue:int):int {
    return minValue + Math.random() * (maxValue - minValue);
}
  

没有重复值的错误?

如果要从范围中排除某些值(生成唯一值),则可以传递先前的值:

function randomValueWithExclude(minValue:int, maxValue:int, excludeValue:int):int {
    var result:int = randomValueFromRange(minValue, maxValue);
    while (result == excludeValue) {
        result = randomValueFromRange(minValue, maxValue);
    }
    return result;
}

以下是如何使用它的示例。每8秒显示一个新提示,这不等于之前:

var timer:Timer = new Timer(8000);
var count:int = itemXMLList.length();
var currentValue:int = -1;
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();

function onTimer(e:TimerEvent):void {
    currentValue = randomValueWithExclude(0, count, currentValue);
    flashTip.htmlText = mTip.channel.item[currentValue].description.text();
}