Last modified on 21 June 2013, at 12:31

Introduction to ActionScript 2.0/Variables and Data Type

Introduction to ActionScript 2.0
Introduction Variables and Data Type Operators

Key concepts:


  • Data types
  • Primitive data types
  • Literals
  • Data type conversion
  • Composite data types and classes
  • Variables
  • Variable declaration
  • Variable initialisation (for primitive data types)

Before we start doing any programming work, we need to work on our basics. Let's start with data types and variables.


What is a data type?Edit

When we work on a Flash project, there are a lot of 'entities' we need to manage: numbers, text, pictures, and so on. A data type is a type of data in our application.

What is a primitive data type?Edit

A primitive data type is a type of data that isn't based on any other data types. The primitive data types we're going to use are as follows:

  • Number (e.g. 1, 3254, 123.045)
  • String (e.g. "My name is Joe Bloggs", "ActionScript")
  • Boolean (true or false)

ActionScript's data types are much simpler than most computer languages. For example, we don't have classes like 'int' or 'double' (like in Java) for numbers; any kind of number can be placed in the Number class. Also, there is no 'char' type - String is already a primitive data type, and if we want to store a character, we can put it in a string. There's only one kind of string: none of that CHAR/VARCHAR business in SQL.

Note the double quotes around the strings. One should always enclose a string with double quotes. The double quotes tells Flash that it's a string, not a variable name or anything else. true is a Boolean value, while "true" is a string.

The examples above are all examples of literals, which means they never change and aren't contained in a variable. For example, you can't change the value of 3254, can you?

How can I convert between primitive data types?Edit

DataType(Value) is used to convert between primitive data types:[1]

  • To convert a number or Boolean to a string, type String(Value). For example, String(3) yields "3", String(false) yields "false", and String(true) yields "true".
  • To convert a Boolean or string to a number, type Number(Value). For example, Number(true) yields 1, Number(false) yields 0, and Number("12") yields 12.
  • To convert a number into a Boolean, type Boolean(Value). Boolean(0) yields false and Boolean(1) yields true.

You can't convert from string to Boolean.

Sometimes, primitive data types just aren't enough. we need to use classes.

What is a composite data type?Edit

Any data type that is not of a primitive data type is a composite data type. Composite data types are called classes.[2] The Object class is a prototypical object that has nothing inside it whatsoever. Under Object, we have many different classes. We will make our own classes in the third section. For now, we'll stick to using Flash's built-in classes.

Flash has a lot of built-in classes. We will discuss each class in detail in the second section. The most important ones to know are the MovieClip and Array classes.

Whenever we have an object of a particular class, we say that the object is an instance of the class. The name of an instance is, surprisingly enough, called an instance name.

What is a variable?Edit

Every entity, of every type, can be stored in a variable

Consider a variable a 'jar'. This jar can be used to store a lot of things. Sometimes, it stores an actual thing; other times, it stores a link to another thing that is too big to put into a jar. (We'll talk about this in the next chapter.)

How can I define a variable?Edit

Now that you've got this far, you deserve to start your first ActionScript project!

The long wayEdit

Open Flash and type in the following code in the first frame, then press CTRL+ENTER to compile and run the script:

Code Result
var Hello:String;
Hello = "Hello world!";
trace(Hello);

Hello world!

In human language, that means 'Create a new String called 'Hello', give it the value "Hello world!", then print Hello on the output box.

The first line is explained as follows:

  • The keyword var is put at the beginning of a line whenever you want to define a variable.
  • Hello is the name of your variable. (The technical term for a variable's name is identifier, but that is unnecessary jargon and we won't use that in this book.)
  • String is the data type of your variable. (The process of giving your variable a data type is called strong typing your variable.)[3]
  • The semi-colon is added to the end of each statement. A statement tells the computer to do something. In human language, 'Apples are green' is not a statement; 'eat that apple' is a statement. [4]

As you have probably noticed, the syntax for declaring a variable (i.e. telling the computer to set aside some of its precious RAM to store your variable) is as follows:

var variableName:DataType;

Now let's take a look at the second line. The equal sign is called the assignment operator It is used to assign a value to a variable. In this case, since we're assigned the words 'Hello world!' to the variable.

The third line is a function which prints the words 'Hello world!' on the output screen.[5] A detailed explanation of functions will be given in the chapter on functions. For now, just take it for granted.

A shortcutEdit

There's a quicker way to declare a variable and assign a value to it.

Code Result
var Hello:String = "Hello world!";
trace(Hello);

Hello world!

This is called initialising a variable.

The syntax for initialising a variable is as follows:


var VariableName:DataType = Value;

How should I name my variable?Edit

Like most programming languages, ActionScript does not allow certain names.

  • All variable names must start with a letter, an underscore (_) or a dollar sign($). Numbers are also allowed after the first character. No other characters should be used.
  • From Flash Player 7 on, variable names are case-sensitive. That means thisVariable and thisvariable are two distinct variables.
  • Reserved words (words that are used in Flash syntax) cannot be used as variable names.

Here's a list of reserved words:

  • add
  • and
  • break
  • case
  • catch
  • class
  • continue
  • default
  • delete
  • do
  • dynamic
  • else
  • eq
  • extends
  • false
  • finally
  • for
  • function
  • ge
  • get
  • gt
  • if
  • ifFrameLoaded
  • implements
  • import
  • in
  • instanceof
  • interface
  • intrinsic
  • le
  • it
  • ne
  • new
  • not
  • null
  • on
  • onClipEvent
  • or
  • private
  • public
  • return
  • set
  • static
  • super
  • switch
  • tellTarget
  • this
  • throw
  • try
  • typeof
  • undefined
  • var
  • void
  • while
  • with


These words may be used in future releases of Flash, so it's best not to touch them, either, lest your application should go wrong in future versions of Flash Player.

  • as
  • abstract
  • Boolean
  • bytes
  • char
  • const
  • debugger
  • double
  • enum
  • export
  • final
  • float
  • goto
  • is
  • long
  • namespace
  • native
  • package
  • protected
  • short
  • synchronized
  • throws
  • transient
  • use
  • volatile

In addition, according to naming conventions, variables should always start with a lowercase letter and consist of words smashed together using CamelCase. For example, instead of $Animalspecies, you should name the variable animalSpecies. There is an exception to this rule, which will be discussed later.

An additional convention for Boolean variables is to use a verb of the third person at the beginning. The most common type is verb to be + complement, such as isHappy, isPaused, etc. Other examples of good Boolean names include likesCheese, hasPimples, and so on.

How can I delete a variable?Edit

To delete a variable, we need to use the word 'delete':

Code Result
var someString:String = "Hello";
delete someString;
trace(someString);

undefined

In this example, we have created deleted the variable 'Hello'.

Note that delete is often problematic. We will discuss this in brief in the second section, and in detail in the third section.

ConclusionEdit

Now that you have an idea how variables work, let's move on to the second chapter: Operators.

NoteEdit

  1. It can also be used to cast an object into another class, but that will come much later in the book.
  2. In ActionScript, primitive data types are 'wrapped' in the Number, String and Boolean classes respectively.
  3. In ActionScript 2.0, it is acceptable to write such a declaration:
    var Hello;
    Hello = "Hello world!";
    trace("Hello world!");
    

    In this code, the variable's data type is not defined. However, this is a frequent source of error for programmers who may then assign something else - like a number - to Hello. To avoid this error, it is highly recommended that you always declare the data type so that Flash will give you an error when you make such a mistake.

  4. The semi-colon is not required in ActionScript (unlike some languages like PHP), but it is strongly recommended that you add it.
  5. The output screen will pop up during runtime if you don't already have it in place.