Variables, Data types, Classes, Properties and Methods in ActionScript 3.0

By Blue_Chi | Flash CS3 | ActionScript 3 | Beginner

This tutorial will introduce you to the very basic tools for ActionScript 3.0. This will hopefully help those with no programming knowledge at all to know the fundamental tools that are used in every single project, namely: variables, data types, classes, properties and methods. You are not expected to have any experience with Flash or ActionScript to follow this tutorial.

We are going to explore the following topics in this tutorial one by one:

  1. Variables
  2. Data Types
  3. Classes
  4. Properties
  5. Methods

Variables

Variables are simply data containers. In order for your program to remember anything you must use a variable to store that piece of information. Variables are created using the var keyword followed by identifier - i.e. the name you would like your variable to be called by. In this code below we will create a new variable called myPhoneNumber:

var myPhoneNumber;
Even though not strictly required, you should always end any code line you write with a semi-colon ;. This could be understood like a period . sign for a sentence in human writing. The ; tells Flash that you line of code has ended.

There are a few rules which must be remembered when creating new variables, the most important of which is that your variable name must not start with a number and that the name itself must not be a reserved word. These are words which are used in ActionScript for other purposes, for example, you cannot call your variable delete, new or var itself. It is also recommended but not strictly required that your variable name does not start with a capital letter, because names that start with a capital letter are expected to be names of classes. We will talk about these later.

Now that you have created your variable, you can use it to store any information you want by using the assignment sign =. For example, if you would like to to store a number as the value of the myPhoneNumber variable, you would do that by adding the following line to your code:

var myPhoneNumber;
myPhoneNumber = 123456;

If you would like to change the value of the myPhoneNumber variable you can always use the = operator at anytime to put a new value:

var myPhoneNumber;
myPhoneNumber = 123456;
myPhoneNumber = 987654
; //This is the new value stored in your variable.
It is possible to write comments with your code to help you remember stuff by using two slash signs "//". The slash signs and whatever comes after them on the same line will not be read by Flash.

If you would like to delete the value stored in your variable, you can do that by setting the value to null using the = operator:

var myPhoneNumber;
myPhoneNumber = 123456;
myPhoneNumber = 987654;
myPhoneNumber = null;

These are the basics which you need to know about creating variable. You should also be aware of the fact that many programmers combine the creation and assignment commands in a single line to save time and effort. So instead of writing:

var myPhoneNumber;
myPhoneNumber = 123456;

They would simply write:

var myPhoneNumber = 123456;

These two blocks of code above mean exactly the same thing. You may choose to write your code using whichever method you like.

Data Types

The value which we stored in our previous variable was a Number. Number is a specific data type which includes any number you can think of. There are many other types of data in ActionScript beside numbers. Some of the basic data types such as Number include:

In addition to these basic data types, ActionScript has some complex data types such as TextField, MovieClip, Tween, and many other ones. Each of these data types has additional attributes and may perform a number of tasks.

The examples seen earlier showed our variable store a Number. Flash by default allows its users to store any data type they wish at any time. So for example you can have your variable start with a number:

var myPhoneNumber = 123456;

And then use the same variable to store a name:

var myPhoneNumber = 123456;
myPhoneNumber = "Blue_Chi";

This action is not recommended at all and can make it extremely hard to debug problems with a program. For this reason, Flash has a feature to stop variable from being using for multiple data types and instead restrict them to a single data type. You can do this by using the colon sign : along with the data type when a variable is defined:

var myPhoneNumber:Number;

Or if you want to assign your value at the same time you can do it this way:

var myPhoneNumber:Number = 123456;

Attempting to insert any data type other than a number in this variable will generate an error whenever you try to test your movie. This will help you discover problems with your code quickly. This will also help explain your code to others if you are working with a group of developers. Make sure that you always specify the data type of your variable when you create it.

Classes

We have not used any classes yet in our tutorial. Classes are the building blocks for all ActionScript projects. Think of these as templates of objects you want to use, whether it is a TextField, a MovieClip, a Button, or any other object.

In order to use any object in ActionScript you must create an instance of that object from its class and save it in a variable. This process is called instantiation and requires using the new keyword in the following generalized code:

var myVariableNam:DataType = new Class;

This means practically that if you want to create a new MovieClip you will have to use the new keyword to create it this way:

var myMovie:MovieClip = new MovieClip();
Always remember to use the two brackets () after the class name to create the instance.

The data type of our object here is a MovieClip because we are going to put a MovieClip inside the variable. In the majority of cases, the Data Type will be the same as the Class name.

The same technique can be used to create any other object, for example a TextField:

var myText:TextField = new TextField();

It is worth noting that instantiating an object does not mean that the object will be visible on the screen. In order for an object to be displayed on the screen you must use the Display List API. This topic goes beyond the scope of this tutorial.

Properties

Certain objects, such as MovieClips for example, have properties and attributes which can be configured via ActionScript. Properties are simple attributes of the object, such as its width, its height, its horizontal position and its vertical position. In order for a property to be configured you need to use the dot (.) operator to access the property and the assignment (=) operator to set a new value for it.

For example, if you want to reposition your MovieClip so that it appears at the position 300, 100 when it gets displayed on the screen you would do that by configuring the .x and .y properties of the instance you have. Here is an example:

var myMovie:MovieClip = new MovieClip();
myMovie.x = 300;
myMovie.y = 100;

Each class provides its instances with a different set of properties depending on the nature and purpose of the class.

Methods

In addition to properties, some objects also have methods, or functionality actions that the object can perform. If your object is a sound object, you can tell this object to play the sound. Any action that the object can perform is called a method. For example, we can create a sound object and tell it to play:

var mySound:Sound = new Sound();
mySound.play();
All methods end with two brackets (). If you do not specify these two brackets your action will not be executed.
For more information on playing Sounds in AS3 make sure you check our tutorial on that topic.

Some methods require you to provide them with additional information for the action to be executed. For example, the Loader Class is responsible for loading external graphical elements into your movie, in order for it to perform the .load() method you must specify the URL of the file to be loaded:

var myLoader:Loader = new Loader();
myLoader.load(new URLRequest("picture.jpg"));
For more information on the Loader Class make sure you check our tutorial on that topic.

This was a very short introduction on some of the basic concepts used in ActionScript 3.0. This information by itself might not be enough for you to create something visual yet, but if you want to do that I suggest that you now check our AS3 Basic Flash Website tutorial to learn how to use these techniques in a practical project.

This concludes our tutorial, I hope that you've learnt something new from it. Feel free to post the Republic of Code Forum if you have any comments or questions.

- End of Tutorial