Building Expressions
As we mentioned earlier in this lesson, an expression is a phraseor a collection of variables, numbers, text, and operatorsthat evaluates to a value. To understand this concept, take a look at this example:
To the right of the equals sign, you see 6 * 2this is an expression. When this script is executed, the expression 6 * 2 is replaced by the result of 6 multiplied by 2, or 12. Thus, oneDozen equals 12.An expression can contain variables, arrays, even function callsanything that, when evaluated, returns a value. For example:
var oneDozen:Number = 6 * 2;
The value of total is based on the result of adding the variable subTotal to the variable tax. The expression is subTotal + tax.
var total:Number = subTotal + tax;
The value of discount is based on the result of multiplying the variable totalPrice by the value of the third element in the employeeDiscount array; totalPrice * employeeDiscount[2] is the expression.Here:
var discount:Number = totalPrice * employeeDiscount[2];
the value of japaneseYen is based on the value returned by a call to the convertToYen() functionmaking the function call itself the expression.
var usDollars:Number = 10;
var japaneseYen:Number = convertToYen(usDollars);
NOTEFor more information about functions, see Lesson 5, "Using Functions."Expressions are used to do the following:Set variable valuesSet array element valuesDetermine whether conditions are being met (using the comparison operators)Dynamically name movie clips, variables, and objectsCall functions dynamicallyAnd more
Expressions enable you to avoid hard-coding values that will remain the same no matter what. By assigning and manipulating values via expressions, you can make the data used by your scripts dynamicresulting in more interactive projects. Many of the scripts in this book rely on expressionsthat's because without them, your project plays back in exactly the same way each time it's viewed.
• Table of ContentsMacromedia® Flash MX 2004 ActionScript: Training from the SourceBy
Derek Franklin, Jobe Makar Publisher: Peachpit PressPub Date: November 19, 2003ISBN: 0-321-21343-2Pages: 636
Sure, you can use Flash MX 2004 without being a master programmer, but as any Flash developer worth his or her salt will tell you, you''''re not tapping all of its power unless you''''re taking advantage of its scripting language "ActionScript 2.0" which offers a more robust programming model and better object-oriented programming support than ever before. Here to take the fear factor out of learning it are Flash veterans and best-selling authors Derek Franklin and Jobe Makar, who demonstrate that scripting is an instinctual process you already know by translating real-life activities into ActionScript scripts. In these pages, you''''ll find methodologies and techniques for building over 40 real-life Flash ActionScript projects, including sample games, applications, Web sites, and more. New in this edition are coverage of ActionScript 2.0, Web services, Components, Printing, Video, and more. On the companion CD, you''''ll find all the project files and images you need to complete each project.