1. Computing

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

By

8 of 9

Randomizing the Display Text
Ask the Magic 8-Ball: Using Flash to Create a Magic 8-Ball Application
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.

Related Video
Adobe Photoshop Magic Eraser Tool

©2014 About.com. All rights reserved.