THE FLASH 4 BIBLE (IDG BOOKS)  back to
FLASH 4 ACTIONSCRIPT EXPRESSIONS  

flash 4 actionscript expressions
Flash uses the term "expression" to refer to two separate kinds of code fragments in ActionScript. An expression is either, 1) a phrase of code used to compare values in a Conditional or a Loop (these are known as "conditional expressions") or 2) a snipit of code that is interpreted at run-time (these are known as "numeric expressions" and "string expressions").

Numeric and string expressions are essentially just segments of ActionScript code that are dynamically converted to their calculated values when a movie runs. For instance, suppose you have a variable, y, set to a value of 3. In the statement x = y + 1, the "y + 1" on the right side of the equal sign is an expression. Hence, when the movie runs, the statement x = y + 1 actually becomes x = 4, because the value of y (which is 3) is retrieved (or "interpreted") and the calculation 3 + 1 is performed. Numeric and string expressions are an extremely potent part of ActionScript because they allow nearly any of the options for Actions to be set based on mathematical calculations and external variables rather than requiring fixed information. Consider these two examples: 1) the "Frame" option of a Go to Action could be set as an expression that returns a random number in a certain range, sending the movie to a random frame and 2) the URL option in a Get URL Action could be made up of a variable that indicates a server name and a literal string which is the file path-to change all the URLs in your movie from a staging server to a live server you'd just have to change the value of the server variable. Anywhere that you see the word "expression" in any Action options or you see the little "abc" button, you can use an interpreted ActionScript expression to specify the value of the option. Just enter the code, then click the "abc" button and select the expression setting, the two lines (=) or shift click.

Within the context of ActionScript code itself, that little "abc" button plays an extremely important role: if you write an expression in a text field but do not change the button to the expression setting, the code will not be executed. Instead, the actual string of text that makes up the code segment will be used as the value of the parameter in question. Even if you just want to assign one variable value to another variable, you must set the button to expression so that the second variable is interpreted. Here's an example: suppose you have two variables: x = 5, and y = 10. If you use Set Variable to make x = y but forget to set the "abc" button to expression, you're actually literally setting x equal to the letter "y", not to the number "10". In your code, you can see the difference: expressions are not quoted, and strings are quoted (eg. x = "y" sets x to the string "y", but x = y sets x to the value of the variable y). Be especially careful to set the "abc" button correctly when assigning dynamic variable values and when using the Trace Action to return variable values.

To use a string inside an expression, simply add quotation marks around it. Anything surrounded by quotation marks will be taken as a literal string. For example, the conditional: if (status eq ready) will wrongly check to see if the value of the variable status is the same as the value of the non-existent variable ready. The correct conditional would check to see if the value of status is the same as the string "ready" by quoting it, as in: if (status eq "ready").

 

about this info
this documentation is a short excerpt from colin moock's section of the 600 page book "the flash 4 bible", (idg books). to order, or for general information about the book, visit colin's flash 4 bible page.

revision history
november 30, 1999: created.
december 1, 1999: posted.