Fortune-Teller
The Magic 8-ball could take user input, but didn’t really do anything with it; the input text box was more for the user’s benefit/visualization than anything else. This time we’re going to take it a step further, though, and add user input to help generate the results in a mock “fortune telling” application. The application takes user input and strings it together in one of various randomly selected but preset sentence structures.
The first thing to do, as always, is set up the artwork/backdrop for your script.

Add input text boxes for the user to input the words that they want to feed into the script. I just decided there’d be two nouns, one adjective, one adverb, one verb, and a name, but you can customize it however you see fit.

Make sure to assign both an instance name and a variable name to each text box. You may also want to make the outlines visible so users know where they should type.

Now, to display the “fortune”, create a dynamic text box on your stage and assign an instance name to it. As usual the text that you put inside it as a placeholder doesn’t matter, as we’ll be dynamically overwriting it.
If you intend to apply any transformations to your text at all, don’t forget to embed the font outlines.
Convert the dynamic text box into a movie clip symbol (F8) ; just like with the Magic 8-ball, this script keeps the movie clip paused on a blank frame and doesn’t play beyond that frame until user action (in this case pushing a button) causes the play head to advance.

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.

The last thing to do is return to your main stage/timeline and create a button to move the playhead of the movie clip ahead to the second frame so that the script runs:
on (release) {
_root.fortunte.gotoAndPlay(2) ;
}

Things to remember:
- Make sure to include the full path name of all variables to the root level (such as _root.name instead of name) to make sure that Flash knows where to look for your information.
- Don’t forget that you’re using the text property of the dynamic text boxes to fill in the dynamically generated message.
- Everything that isn’t a variable inside each dynamically generated message needs to go inside quotes – yes, even spaces and punctuation; you must define everything, and make sure that every time you switch from variable to text or even variable to variable, it must be concatenated using the + sign.