xml - AS3 Duplicate function -
so playing little code , attempting duplicate dynamic text flashtip2 show different values xml like: return (math.floor(math.random() * (0 + 0 + 0)) + 13); - on dynamic text flashtip2. how can duplicate without errors of duplicated values? code is:
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")); /* loads xml */ function loadxml(e:event):void { xmldata = new xml(e.target.data); parsedata(xmldata); } /* gets data 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(); }
getting node currency
to exact node needed currency, search currency code. if currency can't found in list, function return null;
function getnodebycurrency(currency:string, currencydata:xml):xml { var items:xmllist = currencydata..item; var i:uint, len:uint = items.length(); (i; < len; ++i) { if (string(items[i].title).indexof(currency) != -1) { return items[i]; } } //currency isn't found return null; }
usage:
trace(getnodebycurrency("usd", xmldata)); trace(getnodebycurrency("cad", xmldata).description);
getting random value predefined range.
if want random value range, should change function bit:
function randomvaluefromrange(minvalue:int, maxvalue:int):int { return minvalue + math.random() * (maxvalue - minvalue); }
without errors of duplicated values?
if want exclude value range (generate unique value), can pass previous value:
function randomvaluewithexclude(minvalue:int, maxvalue:int, excludevalue:int):int { var result:int = randomvaluefromrange(minvalue, maxvalue); while (result == excludevalue) { result = randomvaluefromrange(minvalue, maxvalue); } return result; }
here example how use it. every 8 seconds show new tip, isn't equal previous:
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(); }
Comments
Post a Comment