1. Home
  2. Computing & Technology
  3. Animation

Flash Fortune-Teller/Random Sentence Generator with User Input

By Adrien-Luc Sanders, About.com

7 of 8

Now double-click the movie clip to edit it, and on your timeline:
  • Copy your first keyframe to a second keyframe;
  • Clear your first keyframe to leave it blank and insert a basic stop[/link] on that frame.

    All of what we’ve done so far is just setup; now we just need to add the script that does the real work. This script goes on frame 2 of the movie clip:

    fortunevar=Math.ceil(Math.random()*3) ;
    if (fortunevar==1){
    _root.fortune.fortunetext.fortunedisplay.text="Your " + _root.noun1 + " will " + _root.adv + " " + _root.verb + " the " + _root.adj + " " + _root.noun2 + " " + _root.name + ".";
    }
    if (fortunevar==2){
    (etc….I think you get the idea at this point, and I’m low on space.)

    This is, again, just like the Magic 8-ball in that it generates a random number between 0 and 1 using Math.random, rounds it to the next highest integer using Math.ceil, and then multiplies by the number of options to get a number anywhere between 1 and X (with X being how many options you have, 3 in my case). Then it uses if statements to check the value assigned to the random number generated, and when one tests as true, performs the function inside.

    What I’ve done is set up a concatenation of text plus variables, arranging the variables gathered from the input text boxes to fill in the blanks in standard sentences. For each set of variables, there are three possible outcomes for how the sentence will be arranged.

  • Explore Animation
    About.com Special Features

    Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

    Easy ways to connect two computers for networking purposes. More >

    1. Home
    2. Computing & Technology
    3. Animation
    4. Flash Animation Tutorials
    5. Flash Fortune-Teller/Random Sentence Generator with User Input

    ©2009 About.com, a part of The New York Times Company.

    All rights reserved.