Flash Actionscript
ActionScript Variables
ActionScript is supposed to be a programming
Program => something which receives some input,
performs some processing, and does some output.
Our Flash animations are interactive
 input = button presses, mouse events
 output = graphics on screen
ActionScript Variables
So it is a programming language of sorts but so far not
a very flexible one for two reasons:
 1. Forms of input are a bit limited
2. It can’t remember any of its input … merely
respond immediately.
I think we need some variables!
ActionScript Variables
A variable is a memory location in which a program
can store data values.
These values can change constantly … when a
program needs to find out the current value it just looks
it up. In order to be able to look up the right one it
associates a unique name with each variable (chosen
by the programmer).
Variables and Data Types
You can explicitly declare the object type of a variable
when you create the variable, which is called strict data
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.
Because data type mismatches trigger compiler errors,
strict data typing helps you find bugs in your code at
compile time and prevents you from assigning the wrong
type of data to an existing variable.
Variables and Data Types
ActionScript has the several basic data
types including:
• the set of integers from
-2,147,483,648 to 2,147,483,647
• the set of integers from 0 to 4,294,967,295.
• Use for special circumstances that call for nonnegative integers. E.g., to represent pixel colour
values, because the int data type has an internal
sign bit that is not appropriate for handling colour
Variables and Data Types
• Can represent integers, unsigned integers, and
floating-point numbers.
• However, to maximize performance, you should
use the Number data type only for integer values
larger than the 32-bit int and uint types can store
or for floating-point numbers.
• To store a floating-point number, include a decimal
point in the number. If you omit a decimal point,
the number will be stored as an integer.
Variables and Data Types
• true/false
• The Object data type is defined by the Object
• The Object class serves as the base class for all
class definitions in ActionScript.
• The String data type represents a sequence of 16bit characters.
ActionScript Variables
Declaring a variable:
 var catName:String;
You can then assign a value - done using an
assignment statement which uses the
assignment operator which, conveniently, is an
= sign.
 catName = "Pirate Eye";
ActionScript Variables
Or you can do both together:
So for example the following are all valid:
var hisName:String = “Pete”;
var hisAge:int = 32;
var gameOver:Boolean = true;
ActionScript Variables
To view the value of a variable, use the trace()
statement to send the value to the Output panel. Then,
the value displays in the Output panel when you test
the SWF file in the test environment.
E.g., trace(hoursWorked) sends the value of the
variable hoursWorked to the Output panel in the test
Add the following code to the previous example:
var xPos:int = mouseX;
ActionScript Variables
 We can also generate values for variables by using
mathematical expressions e.g.
var myAge:int = 42;
var myResult:int = myAge - 10;
myAge = myAge+1;
var newVar:int = myResult * myAge;
We can also add variables which hold things other than
numbers e.g.
var firstPart:String=“This is ”;
var secondPart:String =“a sentence.”;
var sentence:String = firstPart + secondPart;
Don’t mix types in
expressions though!
ActionScript Variables
Let’s look at an example of using variables to store text inputted
from the screen.
Open a new movie and select the Text Option. Select Input Text from the
drop-down menu and click once on the stage to insert a textfield.
Give your textbox an instance name in1.
Check the border box to make Flash draw a box around the textfield.
ActionScript Variables
If we are going to read some text in from the screen
we are going to need to store it somewhere (i.e. we
need a variable)
Create a new layer and call it actions.
Select Frame 1 of the actions layer and open the actions panel.
Type in:
var myTextVar:String = In1.text;
We get a box into which we can type stuff. That stuff
can be referred to as In1.text. We have now asked
Flash to store In1.text in the string variable
myTextVar. Let’s now make an output box to make
sure this is happening.
ActionScript Variables
Click on a blank part of the stage to deselect the input box. Click on the Text Option
again and select Dynamic Text. Make sure border and Selectable are
With the Text tool, add a new text field directly below the existing one. Give this one
the instance name Out1.
Select frame 1 of the actions layer and open the actions panel.
Add the following to the existing code:
Out1.text = myTextVar;
So we have told Flash to display the contents of in1 in
the output textbox.
Test the movie – why doesn’t it work?
ActionScript Variables
10. Add a a frame to layer 1 and keyframe to frame 2 of your actions
11. Select the keyframe and open the actions panel
12. Type in the following code:
Variables and Scope
A variable's scope refers to the area in which
the variable is known (defined) and can be
A global variable is one that is defined in all
areas of your code, whereas a local variable is
one that is defined in only one part of your
Variables and Scope
In ActionScript 3.0, variables are always
assigned the scope of the function or class in
which they are declared.
A global variable is a variable that you define
outside of any function or class definition.
Global Variables
E.g., the following creates a global variable
strGlobal by declaring it outside of any function.
var strGlobal:String = "Global";
function scopeTest()
trace(strGlobal); // Global
trace(strGlobal); // Global
This shows that a global variable is available both
inside and outside the function definition.
Local Variables
You declare a local variable by declaring the
variable inside a function definition.
The smallest area of code for which you can
define a local variable is a function definition.
A local variable declared within a function will
exist only in that function.
Local Variables
E.g., if you declare a variable named str2
within a function named localScope(), that
variable will not be available outside the
function localScope()
var strLocal:String = "local";
trace(strLocal); // error because strLocal is
//not defined globally
Variable Scope
If the variable name you use for your local
variable is already declared as a global
variable, the local definition hides (or shadows)
the global definition while the local variable is in
The global variable will still exist outside of the
Variable Scope
E.g., the following creates a global string
variable named str1, and then creates a local
variable of the same name inside the
scopeTest() function.
var str1:String = "Global";
function scopeTest ()
var str1:String = "Local";
trace(str1); // Local
trace(str1); // Global
Variable Scope
The trace statement inside the function outputs
the local value of the variable, but the trace
statement outside the function outputs the
global value of the variable.
Related flashcards
Create Flashcards