1. Home
  2. Computing & Technology
  3. Animation

Ask the Magic 8-Ball: Using Flash to Create a Magic 8-Ball Application

By , About.com Guide

8 of 9

Now comes the complicated part; on the second frame of the layer containing the dynamic text field, you’ll want to insert a keyframe and add code similar to the following:
ballvar=Math.ceil(Math.random()*8) ;
if (ballvar==1){
_root.ballanimated.balltext.text="YES";
}
if (ballvar==2){ _root.ballanimated.balltext.text="NO";
}
if (ballvar==3){ _root.ballanimated.balltext.text="MAYBE";
}
if (ballvar==4){
_root.ballanimated.balltext.text="ASK ME LATER";
}
if (ballvar==5){
_root.ballanimated.balltext.text="NOT A CHANCE";
}
if (ballvar==6){
_root.ballanimated.balltext.text="SIGNS LOOK GOOD";
}
if (ballvar==7){
_root.ballanimated.balltext.text="WHO KNOWS?";
}
if (ballvar==8){
_root.ballanimated.balltext.text="ASK YOUR SELF";
}
I have eight options, so I used Math.random to generate a random number between 0 and 1 (such as .1, .2, .45689), then multiplied it by 8 to get a number greater than one. Then I used Math.ceil on that number to round it up to the next whole integer, before assigning that integer to the variable ballvar.

The series of if statements following tests the value of ballvar and, depending on what it is, displays a certain message using the text properties of the dynamic text field once the animation passes through frame two. All of this is triggered by nothing more than the playhead passing through frame two on the timeline, which means that the value of ballvar resets every time the movie clip starts again from the beginning.

Explore Animation
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. Animation
  4. Flash Animation Tutorials
  5. Ask the Magic 8-Ball: Using Flash to Create a Magic 8-Ball Application

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

All rights reserved.