//Created by Kinsey Fobes www.fobes.com on 10/8/09 //************************************************************* //This is the program for a Flash based quote banner //It reads quotes of up to 2 lines each from an XML file stored in the //same directory as the swf file. // //The stage is set to 758x88 pixels to match requirements from the web page //it contains 1 movieClip called myBanner. And that movieClip contains //a dynamic text box called myText. It's that movieClip and textbox that we //are manipulating here //************************************************************* // package { import flash.display.*; import flash.events.*; import flash.net.*; import flash.utils.Timer; public class QuoteBanner extends Sprite { public var quotes:XMLList;//Holds all of the quotes private var urlLoader:URLLoader; private var lastQuoteIndex:int=0; private var myTimer:Timer = new Timer(100);//Timer for the tween action private var myWaitTimer:Timer = new Timer(1000);//Timer between the different quotes private var currentQuote:int=-1;//Set to negative number, so first quote is random private var waitDelay:int;//Holds the time between quotes var quoteNumber:int;//How many quotes are loaded var quote1:String;//String for fist part of quote var quote2:String;//String for second part of quote if any var scaleText:int;//holds random number between 0 and 1, 0 means scaling of quote text public function QuoteBanner() { //Main function, loads quotes. var urlRequest:URLRequest = new URLRequest("quotes.xml"); urlLoader = new URLLoader(); urlLoader.addEventListener(Event.COMPLETE, loaderDone); urlLoader.load(urlRequest); } private function loaderDone(e:Event):void { //OK, Quotes are loaded, now start displaying them. quotes = new XMLList(urlLoader.data); quoteNumber = quotes.Quote.length(); var myTimer:Timer = new Timer(100); //Set up initial event handler for timers myTimer.addEventListener(TimerEvent.TIMER, fadeIn); myWaitTimer.addEventListener(TimerEvent.TIMER, waitForQuote); displayQuote(); } function displayQuote():void { //Gets a quote and displays it to the screen getQuote(); myTimer.start();//This timer is for displaying the quote } function getQuote():void { //Decide if the quote scales in size in addition to fading in scaleText = Math.random()*2;//0 equals scaling, else no scale myTimer.addEventListener(TimerEvent.TIMER, fadeIn); //If this is the first time it's being called then get a random quote if (currentQuote<0) { currentQuote = Math.random()*quoteNumber; } quote1=quotes.Quote[currentQuote].FirstPart; quote2=quotes.Quote[currentQuote].SecondPart; myBanner.alpha=0;//Set the beginning alpha to invisible var newY:Number=Math.random()*56;//Choose a new vertical position myBanner.y=newY; myBanner.myText.text=quote1;//Load the text box with the first quote //Set up for the next quote in series //We could have chosen a random quote, but that might result in //Some quotes displaying more often than others currentQuote++; if (currentQuote==quoteNumber) { currentQuote=0; } } function waitForQuote(...e) { //Wait for time out to display the next quote waitDelay-=1; if (waitDelay==0) { myWaitTimer.stop(); //remove the current event listener for myTimer myTimer.removeEventListener(TimerEvent.TIMER, fadeOut); displayQuote(); } } function fadeIn(...e):void { //Increase Alpha every time function called myBanner.alpha+=.1; //If scale text is equal to 0 then scale size as well if (scaleText<1) { if (myBanner.alpha<=1) { myBanner.scaleX=myBanner.alpha; myBanner.scaleY=myBanner.alpha; } } //By waiting for alpha to reach 1.5, the text stays on the screen for //awhile before it starts to fade again. if (myBanner.alpha>1.5) { //Ok, the text has been seen long enough, now start to hide it myTimer.stop(); //Keep the same timer but change it's function from //fade-in to fade-out myTimer.removeEventListener(TimerEvent.TIMER, fadeIn); myTimer.addEventListener(TimerEvent.TIMER, fadeOut); //Start the fade out myTimer.start(); } } function fadeOut(...e):void { myBanner.alpha-=.1; //Ok if we're faded out this far, it's gone from the screen and //we can set up for the sentence 2 if it exists if (myBanner.alpha<0) { myTimer.stop(); myTimer.removeEventListener(TimerEvent.TIMER, fadeOut); //Check to see if we've already displayed the second line //of the quote if (myBanner.myText.text != quote2) { //If this is the second line of the quote, //get a new vertical location var newY:Number=Math.random()*56; myBanner.y=newY; myBanner.myText.text=quote2; //Check to see if there is actually text in the second line //If not then skip it. if (quote2.length>0) { myTimer.addEventListener(TimerEvent.TIMER, fadeIn); myTimer.start(); } else { //No second line, so calulate a wait time //for the next quote waitDelay=Math.random()*12+2; //Start the wait timer before getting the next quote myWaitTimer.start(); } } else { //That was the second line, so calulate a wait time //for the next quote waitDelay=Math.random()*12+2; //Start the wait timer before getting the next quote myWaitTimer.start(); } } } } }