<classwork>

Variables

Variables are a way that you can safely store and retrieve information. You can store a variable and it's value and change it at any time. Every variable has two parts, a name and a value.
example:

n=1
box1="Samantha"
dog=true

You will notice in this example that their are 3 different types of values, or data types, as they are called: number, string, and Boolean.

Examples of numbers are 0, 1.5, 7, 49, and 1,908,452. These are values you can apply math to.
x=20

String values are enclosed in quotation marks. They are literal values, which means that they appear in Flash exactly as they are written.
name = "Lara"

x="20" x=20
y="3" y=3
x+y=203 x+y=23

Boolean values are either true or false. This is used to test if a condition exists.

VariablesAreFun=true

if (PasswordCorrect == true){
then proceed
}

notice the double == sign. This checks to see if a variable is equal to a value or a condition. The = sign is use to set variables equal to a value.


A variable's scope refers to the area in which the variable is known and can be referenced. There are three types of variable scope in ActionScript:

Local variables are available within the function body in which they are declared (delineated by curly braces).

Timeline variables are available to any script on that Timeline.

Global variables and functions are visible to every Timeline and scope in your document.

 

***note: in previous versions of Flash, we did not declare our variables with the var keyword, but in Actionscript 2.0 you declare a variable in Flash using the var keyword. And it is useful to define its datatype when you declare it as well (see below for details).

It's a good idea to assign a value to a variable the first time you declare the variable. Assigning an initial value is called initializing the variable, and it's often done on Frame 1 of the Timeline or from within a class that loads when the SWF file begins to play.

 

Local variables

To declare local variables, use the var statement inside the body of a function. A local variable is scoped to the block and expires at the end of the block. A local variable not declared within a block expires at the end of its script. Local variables can also help prevent name conflicts, which can cause errors in your application. It's good practice to use local variables in the body of a function so that the function can act as an independent piece of code. A local variable is only changeable within its own block of code.

For example, the variables i and j are often used as loop counters. In the following example, i is used as a local variable; it exists only inside the function secondframestops():

function secondframestops() {
var i;
for( i = 0; i < 10; i++ ) {
_root.i.gotoAndStop(2);
}
}

Timeline variables

Timeline variables are available to any script on that Timeline. To declare Timeline variables, initialize them on any frame in the Timeline. Be sure to initialize the variable before trying to access it in a script. For example, if you put the code var x = 10; on Frame 20, a script attached to any frame before Frame 20 cannot access that variable.

Global variables

Global variables and functions are visible to every Timeline and scope in your document. To create a variable with global scope, use the _global identifier before the variable name, and do not use the var = syntax. For example, the following code creates the global variable myName:

_global.myName = "George";

***note: DO NOT use var in global variables
var_global.myName = "George";
//syntax error, BAD

***note: DO NOT assign strict data types to variables that you create in the _global scope, because you have to use the var keyword when you assign a data type. For example, you couldn't do:

_global.foo:String = "foo";
//syntax error, BAD


Incrementing Variables

You can have variable add to their value and calculate a new total, or subtract from their values and calculate a new total.

+=

or

-=

ex.
on (release){
myRotation += 10;
}

this adds 10 to the value of the rotation variable on a button press.


Loading External Variables (see data section on previous page)


Defining Variable Types (not supported by Actionscript 1.0, a feature of Actionscript 2.0)

If you do not explicitly define an item as holding either a number, a string, or another data type, at runtime Flash Player will try to determine the data type of an item when it is assigned. If you assign a value to a variable, as shown in the following example, Flash Player evaluates at runtime the element on the right side of the operator and determines that it is of the Number data type:

var x = 3;

Because x was not declared using strict data typing, the compiler cannot determine the type; to the compiler, the variable x can have a value of any type. (See Assigning a data type.) A later assignment might change the type of x; for example, the statement x = "hello" changes the type of x to String.

ActionScript always converts primitive data types (such as Boolean, Number, String, null, or undefined) automatically when an expression requires the conversion and the variables aren't strictly typed.

Strict data typing offers several benefits at compile time. Declaring data types (strict data typing) can help prevent or diagnose errors in your code at compile time. To declare a variable using strict data typing, use the following format:

to define the variable type use the colon after your variable name:

var variableName:datatype;

datatype possibilities: Number, String, Boolean, Array, or Object, or built-in classes that are included in Flash, such as FileReference, or even custom classes that you or other developers have written.

var myVariable:Number = 10;