Creating Expressions in ActionScript Flash

You can write expressions either by manually typing in the primary option fields of ActionScript commands, or by dragging and dropping Actions from Action Booklets in the Actions Panel. There are no scripting wizards in Flash; Flash 5 will not automatically script anything for you. However, it will provide you with Booklets of operators and functions available in Flash 5.

Operators
Operators are used to perform combinations, mathematical equations, and to compare values.

General and numeric operators
These operators are used for common mathematical operations of adding, subtracting, multiplying, and dividing. You can also use these operators to compare numeric values, such as > or <.

String operators
These Flash 4-specific operators are used to declare, join, or compare string literals with other string literals or expressions. If you want to concatenate two variables to create a new variable, use the string operators.

set (“fullName”, “firstName” & “ “ & “lastName”);

Logical operators
These operators join several expressions to create conditions. We discuss these further in the “Checking conditions: If...Else actions” section of this. // Flash 5 syntax below

The ActionScript operators available in both Flash 4 and 5 syntax shows below

Actionscript operators

Actionscript operators

Checking conditions: If...Else actions
Conditions lie at the heart of logic. In order to create an intelligent machine (or application), we need to create a testing mechanism. This mechanism (called a conditional) needs to operate on rather simple terms as well. Remember the true/false tests that you took in grade school? if/else statements work on a similar principle: If the condition is true, then execute a set of actions. If the condition is false, then disregard the enclosed actions and continue to the next condition or action.

You can simply create isolated if statements that do not employ an else (or then) statement. Solitary if statements are simply ignored if the condition is false. Else statements are used as a default measure in case the tested condition proves false.

Else if statements continue to test conditions if the previous if (or else if) was false. Refer to following examples for more insight.

  • Basic if statement: The code between the curly braces is ignored if the condition is false.
  • Extended if/else if/else statement: If the first condition is true, then code immediately after the first condition is executed and the remaining else if and else statements are disregarded. However, if the first condition is not true, then the second condition is tested. If it is true, then its code executes and all other statements in the if group are ignored. If all conditions prove false, then the code between the curly braces of the final else is executed.
  • In production, you could have an if/else structure that assigned the value of one variable based on the value of another, such as:

Do not use a single = sign in a condition, as this will actually set the variable’s value. For example, if you wrote if (x = 1){}, then Flash will actually set x = 1, and not check whether x’s value is equal to 1.

In Normal Mode, you can add an if statement in ActionScript by choosing the if action from the plus (+) button in the top-left corner of the Actions Panel, or by selecting it from the Actions Booklet. In the Condition text field, enter the expression that identifies what circumstance must exist for the statements in your conditional to be executed. Remember that, in your expression, literal strings must be quoted, and the == operator must be used for string or numeric comparisons. To add an else clause, select the first line of the if statement, and then double-click the else or else if action in the Actions Booklet.

You can join two conditions using Logical compound operators such as and (&&), or (||), or not (!), as in:

Loops
A loop is a container for a statement or series of statements that are repeated as long as a specified condition is exists. A basic loop has three parts: the condition, the list of statements to be repeated, and a counter update. There are four types of loops in Flash 5 ActionScript:

  • while
  • do . . . while
  • for
  • for . . . in

Each of these loop types has a specific use. Depending on the repetitive actions you wish to loop, you need to decide how best to accommodate your code with loop actions.

while(condition){ actions }
This loop was called the Loop While action in Flash 4. In this loop type, the condition of the loop is evaluated first, and, if it is true, then the actions within the curly braces will be executed. The actions will loop indefinitely (causing a script error) unless there is a way out of the loop—a counter update. A counter update will increment (or decrement) the variable used in the while condition. Here you see a breakdown of a typical while loop. Note that a variable used in the condition is usually set just before the while action is executed.

while(condition){ actions }

In this example, a variable named count starts with a value of 1. The first time the while action executes, counter’s value is less than (or equal to) 10. Therefore, the actions within the curly braces are executed. The first action in the loop uses the count value to form the name of a Movie Clip instance, clip_1, and alter it’s X Scale property by a value of 100/1 (which is equal to 100). Then, the count variable is incremented by 1, giving it a new value of 2. The while condition is then reevaluated.

The second time the while action executed, count’s value, 2, is still less than (or equal to) 10. Therefore, the actions within the curly braces are executed again. This time, though, the first action in the loop will address the “clip_2” instance’s X Scale property, and make that property’s value 50 (100/2 = 50). Then, count will be incremented by 1, giving it a new value of 3. Again, the while condition is reevaluated. The while condition will continue to execute its nested actions until count exceeds a value of 10. Therefore, clip_1 through clip_10 will show a decrease in X Scale.

do { actions } while(condition);
This type of loop is very similar to the while loop discussed previously, with one important exception: The actions in the do{} nesting will always be executed at least once. In a do . . . while loop, the condition is evaluated after the actions in the loop are executed. If the while condition is true, then the actions in the do{} nesting will be executed again. If the while condition is false, then the loop will no longer execute.

do { actions } while(condition);

In this example, the actions within the do{} nesting will execute automatically without checking any condition. Therefore, the X Scale of “clip_1” will be set to 100, and the count value will increase by 1, giving it a new value of 2. After the actions execute once, the condition is checked. Because the value of count is not less than (or equal to) 1, the loop does not continue to execute.

for (initialize; condition; next) { actions }
The for loop is a supercondensed while loop. Instead of assigning, checking, and reassigning a variable action in three different actions, a for loop enables you to define, check, and reassign the value of a counter variable.

for (initialize; condition; next) { actions }

This for loop does exactly the same as the while loop example we used earlier. When the loop is started, the variable i is given a starting value of 1. A condition for the loop is specified next, i <= 10. In this case, we want the loop to repeat the nested actions until the value of i exceeds 10. The third parameter of the for loop, i++, indicates that i’s value should be increased by 1 with each pass of the loop. Note that this parameter can use ++ (to increase by 1) or -- (to decrease by 1) operators. You can also use expressions like i = i*2 for the update.

for(variableIterant in object){ actions }
The final type of loop, for . . . in, is the most complex looping mechanism. A for . . . in loop does not need a condition statement. Rather, this loop works with a find-and-replace keyword mechanism. Basically, a variableIterant is declared, which is simply a placeholder for a property or position index within an object or array, respectively. For every occurrence of the variableIterant, the actions within the for . . . in {} nesting will be executed. The for . . . in loop can only be used with objects and arrays, and even then, not all properties of this elements can be enumerated.

chapter21_for_variable

In the preceding code example, the word name is used to designate a property of the _root timeline. In this case, we want to change all Movie Clip instances on the Main Timeline to a 50 percent X Scale value. We don’t need to specify the actual target paths of each individual instance the for . . . in loop will search for all instances on the Main Timeline, apply the change, and exit the loop.

Although this might look a bit confusing, it can be more helpful than you can imagine. Have you ever had a bunch of nested Movie Clip instances that all need to play at the same time? In Flash 4, you would have had to use several tellTarget(){} actions, each one specifying the target path. You could use a while loop to shorten the lengthy code, but, even still, you would need to list the specific parts of the each Movie Clip path, as in:

The preceding code block would tell clip_1 through clip_10 to start playing. But what if you didn’t know (or care to remember) all the paths to several differently named Movie Clip instances? For example, if you had a Movie Clip instance named nestAnim with several nested Movie Clip instances with different names (for example, squareAnim, triangleAnim, and circleAnim), then you would have to specifically name these instances as targets. In Flash 5, the for . . . in loop would let you control any and all nested Movie Clip instances simultaneously:

With just three lines of code, all Movie Clip instances in the nestAnim Movie Clip instance will start to play. How? Remember that the variableIterant name is simply a placeholder for a property of the nestAnim Movie Clip object. The for . . . in loop will find every occurrence of an instance inside of nestAnim. And the word name has no significance. We could use a variableIterant myName, and everything would still work fine. Think of the variableIterant as a wildcard in file searches or directory listings in MS-DOS or UNIX

nestAnim[*].play();

Although this syntax won’t work with ActionScript, it does illustrate the processing of a for . . . in loop. Everything and anything that is playable on the nestAnim timeline will play.

break
The break action is not a type of loop it is an action that enables you to quickly exit a loop if a subordinate condition exists. Suppose you wanted to loop an action that hides, at most, clip_1 through clip_10 (out of a possible 20 Movie Clip instances), but you want to have a variable control the overall limit of the loop, as upperLimit does in the following code block. upperLimit’s value could change at different parts of the presentation, but at no point do we want to hide more than clip_1 through clip_10. We could use a break action in a nested if action to catch this:

Break statements, though, should be reserved for catching errors (such as during a debug process) or the need for an immediate exit from the loop.

continue
Like the break action, continue enables you to exit the execution of actions within a loop. However, a continue action won’t exit the loop action. It simply restarts the loop (and continues evaluating the current condition). Usually, you will place a continue action with an if nest—otherwise, it will always interrupt the actions within the loop action. For example, if you wanted to omit a particular value from going through the loop actions, you could use the continue action to bypass that value. In the following code block, we will hide clip_1 through clip_10, except for clip_5:

Adding a loop to your Actions list
To create a loop, add one of the loop-type actions in the Actions Panel, using the plus (+) button in the top-left corner of the panel (or selecting it from the Actions booklet). In the Condition text field, enter an expression that describes the conditions under which the loop should continue repeating. Before the end of the loop, be sure to update whatever the loop relies on in order to continue, usually a counter. If you forget to update a counter, you will be stuck forever in the loop, and Flash will imperiously stop the script from continuing.

Loops in Flash are not appropriate for running background processes that listen for conditions to become true elsewhere in the movie. While a loop is in progress, the screen is not updated and no mouse events are captured, so most Flash actions are effectively not executable from within a loop. Loop Actions are best suited to abstract operations such as string handling (for example, to check each letter of a word to see if it contains an @ symbol) and dynamic variable assignment.

Loops to execute repetitive actions, which affect tangible objects in the movie, should be created as repeating frames in Movie Clips. To create a permanently running process, make a Movie Clip with two keyframes. On the first frame, call the subroutine or add the statements that you want to execute; on the second frame use a gotoAndPlay(1); action to return to the first frame. Alternatively, you can use Flash 5’s new on Clip Even t(enter Frame) handler to execute repetitive actions.

Properties
Properties are characteristics (such as width and height) of movies and Movie Clips that can be retrieved and set. You can use variables to store the current value of a given property, such as:

xPos = _root._xmouse;

which will store the current X position of the mouse pointer (relative to the stage coordinates of the Main Time line) in the variable xPos.

Built-in functions
Flash 5’s ActionScript contains a number of native programming commands known as functions. Among others, these functions include getTimer, getVersion, parseFloat, parseInt, int, string, substring, escape, and unescape. It’s beyond the scope to discuss the practical use of every new function and ActionScript element in Flash 5. We do, however, discuss many built-in functions throughout this part of the Flash Bible.

Creating and calling subroutines
Whether they’re called functions or subroutines, most programming languages provide a mechanism for programmers to create self-contained code modules that can be executed from anywhere in a program. ActionScript supports subroutines by using the new Flash 5 ActionScript function constructor. You can create functions on any timeline, and, just like Movie Clip instances, functions have absolute or relative paths that must be used to invoke them. For example, if you have the following function on a Movie Clip named Functions, located on the Main Timeline:

then to invoke it from another timeline, you would execute it as follows:

_root.Functions.makeDuplicate(“clip”,5);

Executing it would create five duplicates of the Movie Clip instance named “clip”, naming the duplicates “clip_1”, “clip_2”, “clip_3”, “clip_4”, and “clip_5”.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Flash Topics