We've looked at declaring and assigning values to variables and a few code structures that use variables. In order to do anything useful with variables in the way of comparison, calculation, concatenation, or even bit manipulation, we need to see how actionscript operators work, so here are some examples:
| j = j + 1; j++; |
j equals j plus 1 (two different ways to do the same thing) |
| j = 5 + x * y; | j equals x times y, plus 5 (multiplication and division take precedence over addition and subtraction |
| j = (5 + x) * y; | Add 5 to x and multiply the sum by y, then assign that to j (use parentheses to override built-in order of precedence) |
| k = 3 * j++; | k equals 3 times j. j is also incremented (after the assignment) |
| k = 3 * --j; | Decrement j. k equals 3 times the decremented value of j. |
| remainder = 15 % 4; | (modulo operator) remainder = integer remainder when 15 is divided by 4 (=3) |
| y += 3; | shorthand for y equals y plus 3. Other math shorthand operators are -=, *=, /=, %= |
| isFinished = !waiting; | isFinished = true if waiting is false (or false if waiting is true) |
| isFinished = !isFinished; | toggles boolean variable isFinished (if true, set it false, and vice versa) |
| isFinished = maryDone && samDone; | isFinished is true if maryDone is true and samDone is true, otherwise it's false. This is a logical AND operator -- in particular, a short-circuit AND, because execution of the right side of the statement halts as soon as a false value is found. This is important to know if one of the expressions on the right side of the equation is a function -- that function will not be executed if one of the conditions listed before it evaluates to false. |
| shipAnyway = maryDone || samDone; | shipAnyway is true if maryDone is true or samDone is true, otherwise it's false. This is a logical OR operator, and is similar to the short-circuit AND -- execution of the right side of the statement halts as soon as a true value is found, and subsequent functions or expressions do not get evaluated. |
In case you inherit a program written in Flash 4 (poor you) or want to publish a movie to play in Flash 4 (noble you), here are some operators you may need to know about:
| + | This is used for addition
of variables of type Number, or for concatenation of variables of type
String. (It also works to concatenate a String and a Number variable
to produce a String, as in dup = "mcDup" + i) |
| && | This is a logical AND operator,
used to test two or more logical variables or expressions. keepGoing
= (i < nBigValue) && (j < nLoopStop) is an example which sets keepGoing
to true only if both expression conditions are true. |
| & | This a BITWISE AND operator
which takes the AND of 2 or more bits in 2 or more variables. nLower3bits
= 0x07 & bThisByte returns the lower 3 bits of bThisByte. (In Flash
4, this operator was used for concatenation of strings, but is converted
to "add" when brought into Flash 5, 6 (MX) or 7 (MX 2004)). |
| add | This operator is only necessary when concatenating strings in a Flash 5/6/7 movie which must be published to run in Flash 4. For example, sMovie = "cartoon" + i + ".swf" should be written sMovie = "cartoon" add i add ".swf" if the movie is to published in Flash 4 format. The add operator is no longer supported in Flash 8. |
| if (counter > 5)
{ player_mc.gotoAndPlay("endseq"); } |
jump to the frame labelled
endseq in movieclip player_mc if counter is greater than 5 (notice the
condition must be in parentheses, and the action is in brackets) |
| if (x != 5) { gohome(); } |
if x is not equal to 5, execute function gohome |
| if (s != undefined) { gotoAndPlay("continue"); } |
if s has been defined (not undefined), jump to the frame labelled continue (this is useful eg to check and see when the last record has been read from a text file with an unknown number of records) |
| if (counter == cFinal) { bigCounter = 0; } else { bigCounter++; } |
checks to see if the value of counter is equal to the value of cFinal (and sets bigCounter to 0 if so, or increments it if not). Notice that checking for equality requires two == signs! |
| sFrame = x > 2 ? "continue" : "stop"; | This conditional assignment
statement sets variable sFrame to "continue" if x is greater than 2,
or to "stop" otherwise. The format of the conditional part (ternary
operator) is: <expression> ? <value if true> : <value if not true> |
| nRed = nColor >> 16; | Given an RGB color nColor, pull the red part into variable nRed. How? Colors are specified by 3 groups of 2 hex digits each:RRGGBB. Each hex digit is 4 bits, so each color is specified by 8 bits: rrrrrrrrggggggggbbbbbbbb. The ">>" operator shifts bits to the right by the amount specified. So to get the red part (leftmost group of 8 bits), you simply shift all the bits to the right 16 slots (the lower part, the G and B colors, are discarded during the shift). |
| nGreen = (nColor >> 8) & 0xff; | As above, the bits in the color variable nColor are shifted to the right, this time moving everything 8 bits to the right. The lower bits (blue color) fall off the end. We're left with red in the upper 8 bits and green in the lower 8 bits. To keep only the green color, we perform an AND bitmask to keep only the lower 8 bits. |
| cObject.setRGB(nRed<<16 | nGreen<<8 | nBlue); | Looking at things the other way around, this code sets the color of cObject based on individual red, green, and blue colors. The colors are shifted into their correct slots with bitwise operators. (Note that <<16 is the same as * 65536 -- that is, bit-shifting left n bits is the same as multiplying by 2 to the nth power, and bit-shifting right divides. <<1 = *2, <<2 = *4, <<3 = *8, ... <<8 = *256, ... <<16 = *65536). |
Discussed on this page:
operators, increment, decrement, compare, equals, greater than, less than, bit operators, and, or, not, modulo, remainder, bit shift, ternary
Other Resources
Josh Buhler has a nice article on using the ternary operator in Flash.