Loading Random Quotes
      by Ryan Seddon aka Inept | 18 June 2005

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";

  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) {

  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 page to see the explanation of the ActionScript and the setting out of the text file.

Onwards to the next page!


page 1 of 2



kirupa.com's fast and reliable hosting provided by Media Temple.