Tutorials Books Videos Forums

Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

Loading Random Quotes

by Ryan Seddon aka Inept   | filed under Flash and ActionScript

This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.

Loading random quotes into a text field in flash can add a little something extra to your website. Whether it be quotes by famous people, your favourite movie quotes or even your own quotes. In this tutorial you will be shown how to load random quotes from an external text file into a dynamic text field. This tutorial can both be done in Flash MX/2004

Have a look at the example below. Just click on the generate button to load another randomised quote

[ an example of what you will create ]

Here is How

The following steps will take you through the process of creating the above example:

  1. Open up flash MX/2004 and create a new file with the a width of 290 and a height of 90.

  2. Ok now we are going to create 3 layers each with 2 frames: Actions, Button and Text. Your timeline should look like this:

    [ Your timeline should look the same ]

  3. Now we need to create the text box. Make sure the second keyframe on the Text layer is highlighted. Click on the text tool and create a text box of any size, now go to the properties panel and make sure it is a dynamic text box, while where there we will give it an instance name of quote_txt. Once it is create make sure the text box is highlighted and press Ctrl + K to bring up the align panel. Centre it horizontally

[ The properties panel ]

  1. Click on the text tool again, make sure in the properties panel that the text is set back to static text. Click the Button layer so it is highlighted and click below the text field on the stage and type Generate. Click on the text to highlight it and push F8 this will bring up the convert to symbol panel name it gen_btn and make it a button.

  2. Double click on the newly created button to bring it into edit mode. Once there create a new layer. Click on the rectangle tool and set stroke to no colour and fill to any colour and change the alpha to 0%. On the newly created layer draw a rectangle over the top of the text. Now go back to the main timeline.

    [ What your settings should look like in the colour mixer ]

  3. Now we have the stage set up we will add in the ActionScript to make it all work. In the second frame of the Actions layer copy and past this into the Actions panel:

    /****************************************
    **Random Quotes Script*******************
    ****************************************/
    ranQuote = new LoadVars();
    ranQuote.onLoad = function(success) {
      if (success) {
      RanNum = Math.ceil(Math.random()*5);
      ran = this["quote"+RanNum];
      quote_txt.text = ran;
      }
      else {
      quote_txt.text = "The text failed to load due to an error";
      }
    }
    ranQuote.load("Quotes.txt");
    stop();
  1. One more thing to add is some ActionScript to the button so it can generate a new quote. With the generate button selected open up the Actions panel and copy and paste this into it:

    on (release) {
      _root.gotoAndPlay(1);
    }
  1. Save your flash file and test your movie (Ctrl+Enter). You will get the error saying: text failed to load due to an error, why did we get this error? Well we haven't created the text in which the movie will read from.

    Open up a plain text editor something like notepad and copy and paste this:

    Text file
    quote1=Kirupa forums are the best.&quote2=This is a randomly generated quote.&quote3=I am the man with no name...Zap Brannigan at your service.&quote4=Inept is the best.&quote5=Artificial Intelligence is no match for natural stupidity.
     
    Now save this file as Quotes.txt in the same directory you saved your flash file.
  2. Test your movie now, and you should see the one of the quotes show up from the text file. Keep clicking on generate to see other quotes show up.

    That's it, you have completed the tutorial...but of course it isn't over yet. Move along to the next section to see the explanation of the ActionScript and the setting out of the text file.



In the previous section we created the flash file that randomly picked out a quote from an external text file. In this page I will explain the ActionScript used, and also the how to format the text file so Flash can read it correctly.

The ActionScript:

ranQuote = new LoadVars();

This line creates a new LoadVars variable ranQuote a LoadVars variable allows you to load variable to use in flash from an external source in our case a text file.


ranQuote.onLoad = function(success) {
  if (success) {
  RanNum = Math.ceil(Math.random()*5);
  ran = this["quote"+RanNum];
  quote_txt.text = ran;
  }

This bit of code test to see if the text file is loaded and acts accordingly. If the if statement returns success which is a boolean (true or flase), the code in side the if statement will get executed. This is where the it generates the random number and picks out a quote to display. So lets dissect the if statement further and explain what is going on:


RanNum = Math.ceil(Math.random()*5);

This line the variable RanNum is created and it generates a number between 1 and 5 by using Math.ceil() it will round up the number so as you never get a zero any decimal places in your random number. The Math.random()*5 is self explanatory if you want to generate more numbers, you would change the 5 to however many quotes you have in your text file.


ran = this["quote"+RanNum];

We have a random number stored in a variable now, and now we need that number to assign to the quote variable inside the text file. we create the ran array and attach the random number to our "quote" variable.


quote_txt.text = ran;

This is what displays the random quote into the text field by using the instance name, quote_txt, we assigned to it in the previous section. It then takes the array ran and uses that to know which quote to display from the text file.


else {
  quote_txt.text = "The text failed to load due to an error";
}

This bit of code is only executed if the text fails to load and displays the custom error message in the text field. You can of course change the error message to what ever you like.


ranQuote.load("Quotes.txt");
stop();

These second last line is the all important line that loads the actual text file for flash to reference the external variables. The last line is of course self explanatory.


Text File Formatting

Now we have explained the ActionScript, I will show you how to format your text file:

The variables must have no space between in the equals sign(=) and the actual quote you want to be displayed. E.g. quote1=This is a quote

Now when we want to have more than one variable in a single text file they must be separated by an ampersand(&) with no spaces.
E.g. quote1=blahblahblah&quote2=blahblah

Your done if your having problems, download the source file so you can see what exactly went wrong.

If you having any issues don't be afraid to check out the Kirupa Forums and ask any questions you may have.

Cheers!

Ryan Seddon aka Inept


Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.

Your support keeps this site going! 😇

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--