Data refers to the numbers, strings, and other information that you can manipulate within Flash. Using data is usually essential when you create applications or websites. You also use data when you create advanced graphics and script-generated animation, and you might have to manipulate values that you use to drive your effects.
You can define data in variables within Flash, or you can load data from external files or sites using XML, web services, and so on. You can store data in a database and then represent that information in several ways in a SWF file. This can include displaying the information in text fields or components or displaying images in movie clip instances.
Some of the most common kinds of data include strings (a sequence of characters, such as names and passages of text), numbers, objects (such as movie clips), Boolean values (true and false).
A data type describes a piece of data and the kinds of operations that you can perform on it. You store data in a variable below. You use data types when creating variables, object instances, and function definitions to assign the type of data you’re working with. You use many different data types when you write ActionScript.
You can classify different types of data using a data type to assess how you can use the data. For example, you can perform mathematical operations with number data, but you cannot meaningfully rotate a number. A movie clip, on the other hand, can be rotated, but you cannot meaningfully multiply a movie clip by another movie clip.
ActionScript 2.0 enables you to formally specify the data type for variables, parameters, and function return types, and tell Flash what classification of data can be used in that context. For example, if you declare a variable as a String data type then you can assign only strings to that variable. If you attempt to assign a number to the variable Flash will throw a compiler error.
ActionScript has two basic classifications for data types: primitive and complex.
- A primitive value (or primitive data type) is a value that ActionScript stores at the lowest level of abstraction, which means that operations on the primitive data types are generally faster and more efficient than operations carried out on complex data types. For example, Boolean, number, and string values are all primitive data types, as are the values null and undefined.
- A complex value (or complex data type) is a value that is not a primitive value and that references the primitive values. Often, these are called reference data types. Complex values belong to the Object data type or a data type that is based on the Object data type. Data types that define sets of complex values include Array, Date, Button, MovieClip, and XML.
Variables that contain primitive data types behave differently in certain situations than those containing complex types.
- About Variables
(Learning AS 2.0 > Data and Data Types > About Variables)
- Using Variables in a Project (Learning AS 2.0 > Data and Data Types > About Variables > Using Variables in a Project)
- For information on the basic data types that you can use in your Flash applications, see About Primitive and Complex Data Types.
(Learning AS 2.0 > Data and Data Types > About Data Types > About Primitive and Complex Data Types)
A variable is a container that holds information. The following ActionScript shows what a variable looks like in ActionScript:
var myVariable:Number = 10;
This variable holds a numerical value. The use of
:Number in the previous code assigns the type of value that variable holds, called data typing.
The container (represented by the variable name) is always the same throughout your ActionScript, but the contents (the value) can change. You can change the value of a variable in a script as many times as you want. When you change the value of a variable while the SWF file plays, you can record and save information about what the user has done, record values that change as the SWF file plays, or evaluate whether a condition is true or false. You might need the variable to continually update while the SWF file plays, such as when a player’s score changes in a Flash game. Variables are essential when you create and handle user interaction in a SWF file.
Variables can hold different types of data. The type of data that a variable contains affects how the variable’s value changes when you assign that value in a script.
Typical types of information that you can store in a variable include a URL (String type), a user’s name (String type), the result of a mathematical operation (Number type), the number of times an event occurred (Number type), or whether a user has clicked a particular button (Boolean type). Each SWF file and object instance (such as a movie clip) has a set of variables, with each variable having a value independent of variables in other SWF files or movie clips.
To view the value of a variable, use the
trace() statement to send the value to the Output panel. Then, the value appears in the Output panel when you test the SWF file in the test environment. For example,
trace(hoursWorked) sends the value of the variable
hoursWorked to the Output panel in the test environment. You can also check and set the variable values in the Debugger in the test environment.
Data and Data Types > About Variables:
- About Declaring Variables
- About Assigning Values
- About Naming Variables
- Using Variables in an Application
- About Variables and Scope
- About Loading Variables
- Using Variables in a Project
Assigning Data Types:
You can explicitly declare the object type of a variable when you create the variable, which is called strict data typing. If you do not explicitly define an item as holding either a number, a string, or another data type, Flash Player tries to determine the data type of an item when it is assigned (at runtime).
- About Assigning Data Types and Strict Data Typing and its subsection, Assigning a Data Type
- About Type Checking