Hi,
I’ve been messing around with this XML random quote rotators from this site but I’m unable to make it so it doesn’t repeat the same quote twice. It has a tendency to show the same quote 3x in a row.
//importing tween classes
import fl.transitions.Tween;
import fl.transitions.easing.*;
//we create a variable to store the loaded XML
var xml:XML;
//the path to the xml file
var xmlPath:String = "data/quotes.xml";
//we create a loader and when the loading is over we call the xmlComplete function
var loader:URLLoader = new URLLoader();
//a url request for our loader
var req:URLRequest = new URLRequest(xmlPath);
loader.addEventListener(Event.COMPLETE, xmlComplete);
loader.load(req);
//a variable for our tween
var myTween:Tween;
//we need to set a timer
var timer:Timer = new Timer(5000);
timer.addEventListener(TimerEvent.TIMER, changeQuotes);
timer.start();
function xmlComplete(e:Event):void{
xml = new XML(e.target.data);
addQuotes();
randomQuoteTween();
}
//we create arrays to store the quotes and the authors
var quotesArray:Array = new Array();
var authorsArray:Array = new Array();
//we need a random quote number
var randomNumber:Number;
function addQuotes(){
//we loop through the "quote" element of our XML
for each(var item:XML in xml.quotes.quote){
//we fill the arrays with quotes and authors
quotesArray.push(item.content);
authorsArray.push(item.author);
}
}
function randomQuoteTween():void{
//calculating the random number
randomNumber = Math.round(Math.random() * (quotesArray.length - 1));
//filling the text fields
quoteMC.quoteTxt.text = quotesArray[randomNumber];
authorMC.authorTxt.text = authorsArray[randomNumber];
//creating tween effects
myTween = new Tween(quoteMC, "alpha", Strong.easeOut, 0, 1, 3, true);
myTween = new Tween(authorMC, "alpha", Strong.easeOut, 0, 1, 3, true);
}
function changeQuotes(e:TimerEvent){
randomQuoteTween();
}
I figured it requires using shuffle, but I don’t know how to put it in this code. I’m fairly new at this and would appreciate any assistance.
Thanks