One Part of the Sum: ActionScript Variables Flash

After you have a clear understanding of Movie Clip instances and how to control their playback and properties, you can move on to the fundamentals of learning a system of composing more complex scripts with Flash actions. If you are familiar with JavaScript or Director Lingo, then your transition to Flash’s scripting environment will be smoother.

In any scripting or programming language, you will need some type of “memory” device something that can remember the values and properties of Objects or significant data. This type of memory device is referred to as a variable. Variables are named storage places for changeable pieces of data (numbers and letters). One of the first obstacles for designers learning a scripting language to overcome is the concept that variable names in and of themselves have no meaning or value to the computer. Remember that the computer can’t perform anything unless you tell it to. Even though any given scripting language has a certain set of “built-in” properties and functions, variables can simplify our scripting workload by creating shortcuts or aliases to other elements of the ActionScript language of the script. One prime example of a shortcut variable is the pathname to a deeply nested Movie Clip instance, such as:

Once pathToEgg is declared and given a value, then we can reuse it without referring to the lengthy path name, as in:

The important concept here is that you could just as easily have given pathToEgg a different name, such as myPath, or robPath, or whatever word(s) you’d like to use. As long as the syntax and formatting of the expression is correct, then you have nothing to worry about.

Variables in Flash are attached to the timeline of the movie or Movie Clip instance on which they are created. If you create a variable x on the Main Timeline, that variable is available for other scripting actions on that timeline. However, from other Movie Clip timelines, the variable is not directly accessible. To access the value of a variable on another timeline (such as a Movie Clip instance), enter the target path to the clip instance in which the variable resides, a dot (.), and then enter the variable name. For instance, this statement sets the variable x to be equal to the value of the variable y in Movie Clip instance named ball:

x = _root.ball.y;

Whereas this statement sets the variable y to be equal to the value of the variable x on the Main Timeline:

y = _root.x;

String literals
In programmer speak, a string is any combination of alphanumeric characters. By themselves, they have no meaning. It is up to you to assign something meaningful to them. For example, giving a variable the name firstName doesn’t mean much. We need to assign a value to the variable firstName to make it meaningful, and we can do something with it. For example, if firstName = “Susan”, then we could make something specific to “Susan” happen. You can also use much simpler name/value pairs, such as i = 0, to keep track of counts. If you want a specific Movie Clip animation to loop only three times, you can increment the value of i by 1 (for example, i = i + 1, i += 1, and i ++ all do the same thing) each time the animation loops back to the start. Then, you can stop the animation when it reaches the value of 3.

Expressions
Flash uses the term expression to refer to two separate kinds of code fragments in ActionScript. An expression is either (a) a phrase of code used to compare values in a Conditional or a Loop (these are known as conditional expressions), or (b) a snippet of code that is interpreted at runtime (these are known as numeric expressions and string expressions). We discuss conditional expressions later. 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 permit 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 Type 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
  2. The URL option in a getURL 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, you can use an interpreted ActionScript expression to specify the value of the option. Just enter the code, and then check the Expression option. To use a string inside an expression, simply add quotation marks around it.

Anything surrounded by quotation marks is taken as a literal string. For example, the conditional: if (status == ready) wrongly checks whether the value of the variable status is the same as the value of the nonexistent variable ready. The correct conditional would check whether the value of status is the same as the string “ready” by quoting it, as in: if (status == “ready”).

You can even have expressions that indirectly refer to previously established variables. In Flash 5, you can use the new Dots notation (and array access operators) to indirectly refer to variables, or you can use Flash 4’s eval() function (to maintain backward compatibility).

Array access operators
If you have a variable called name_1, you can write the expression _root[“name_” + “1”] to refer to the value of name_1. How is this useful? If you have more than one variable, but their prefix are the same (for example, name_1, name_2, name_3, and so on), you can write an expression with two variables as a generic statement to refer to any one of the previously established variables: _root[“name” + i], where i can be any predefined number.

Eval( )function and Flash 4’s Set Variable
If you want to use old-fashioned ActionScript to indirectly refer to variable names and values, then you have two ways to go about it:

  1. Use the Set Variable action, specifying the variable name as a Slash-notated expression as:
  2. set(“/name_” add i, “Robert Reinholdt”);
  3. Use the eval() function, specifying the variable as an expression:
  4. eval(“_root.name_” add i) = “Robert Reinholdt”;

Variables as declarations
In most scripting languages, you usually don’t have to declare a variable without its value; that is, you don’t need to say variable firstName and then address it again with a value. In Flash, you don’t need to preestablish a variable in order to invoke it. If you want to create a variable on the fly from a Movie Clip to the Main Timeline, you can. Most variables that you use in Flash will be declared in a timeline’s keyframes.

Variables as text fields
Since Flash 4, text could be specified as text fields. A text field can be used as a dynamic text container whose content can be updated via ActionScript and/or the intervention of a server-side script (known in Flash 5 as Dynamic Text), or it can accept input from the user (known in Flash 5 as Input Text).

You can access its properties by selecting a text field and opening the Text Options Panel. In this panel, you can define the parameters of the text variable, including its name. An Input Text field is editable when the Flash movie is played; the user can enter text into the text field. This newly typed text becomes the value of the text field variable. On a login screen, you can create an Input Text field with a name login, where the user enters his/her name, such as Joe. In ActionScript, this would be received as login = “Joe”. Any text that you type into a text field during the authoring process will be that variable’s initial value.


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

Flash Topics