Task: Flying Words

I l @ ve RuBoard

Creating text with ActionScript can allow you to create some interesting effects with almost nothing on the stage or in the Library. In this example, a whole series of text fields is created inside movie clips. These movie clips fly at the screen and disappear. The result looks something like Figure 24.8.

Figure 24.8. Driven by ActionScript, different words fly at the user .

graphics/24fig08.gif

  1. Create a new movie.

  2. Open the frame script so that you can add functions. The first function takes a piece of text and creates a new movie clip that contains a dynamic text field with that text.

     function createText(n,text) {     // create a new movie clip     this.createEmptyMovieClip("text"+n,n);     mc = this["text"+n];     // set the text format     myFormat = new TextFormat();     myFormat.font = "Arial";     myFormat.color = 0x000000;     myFormat.size = 24;     myFormat.align = "center";     // create a new text field     mc.createTextField("myTextField",1,-100,-20,200,40);     mc.myTextField.text = text.toUpperCase();     mc.myTextField.embedFonts = true;     mc.myTextField.setTextFormat(myFormat);     // return reference to this movie clip     return(mc); } 
  3. The createAllText function loops through an array of text and calls createText for each one. It also sets the location of the movie clip to a random place not too close to the edges of the screen.

  4. The scale properties of the movie clip are set to 0. A scale variable is set to a number less than 0. For the first movie clip, it is set to -100. The second is set to -200, and so on.

     function createAllText(textList) {     // loop through array of text     for(var i=0;i<textList.length;i++) {         // create movie clip with this text         mc = createText(i,textList[i]);         // set random location         mc._x = Math.random()*450+50;         mc._y = Math.random()*350+25;         // set scale to nothing         mc._xscale = 0;         mc._yscale = 0;         // set scale variable to negative amount         mc.scale = 0-i*100;     } } 
  5. The init function creates the array of text from a single text variable, using the split command. It then calls createAllText . The number of movie clips is stored in numWords for later use.

     function init() {     // create array of text     var words = "Love,Peace,Destiny,Llamas,Fate,History,Cheese,Rainbows,Tiny Rocks";     var textList = words.split(",");     // create all text movie clips     createAllText(textList);     // remember how many there are     numWords = textList.length; } 
  6. The moveText function loops through each movie clip and increases its scale variable. Clips that have a scale greater than 300 are made invisible. Clips between 0 and 300 are shown at that scale. Clips that have not yet reached 0 are left unchanged.

     function moveText() {     // loop through words     for(var i=0;i<numWords;i++) {         // increase the scale of this movie clip         mc = this["text"+i];         mc.scale += 10;         // hide movie clip when scale is too big         if (mc.scale > 300) {             mc._visible = false;         // set scale of movie clip to scale when it is a positive number         }  else if (mc.scale > 0) {             mc._xscale = mc.scale;             mc._yscale = mc.scale;         }     } } 
  7. The frame script ends by calling its own init function.

     init(); stop(); 
  8. Back at the root level, use a simple shape to create an Actions movie clip. Move it out of sight and attach this script to it:

     onClipEvent(enterFrame) {     _parent.moveText(); } 
  9. Open the Library window and use its own menu to choose New Font. Select the font Arial, or one of your choice. Name it Arial.

  10. Select the Arial font in the Library and use the Library's menu to choose Linkage. Set this Library element's Linkage properties to Export for ActionScript. Give it the Linkage name Arial.

Try the movie. You can compare it to 24flyingtext.fla. Try changing the words, the font, the size, and anything else that you want.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net