Monday, August 6, 2012

Classes and Objects in ActionScript 3

In this lesson, I'll be explaining what classes and objects are. We'll be understanding these terms within the context of ActionScript, but classes and objects really are concepts that are part of most programming languages. So the general idea of what you'll learn here, can apply to other programming languages as well.

What is a class?
In programming, a class is a set of instructions that's used to build objects.

So from classes, we can create objects.

In some sense, a class is kind of like a factory. Factories are where products are built. A car factory produces cars, a toilet factory produces toilets, and so on.

Similarly, classes are used to produce objects.

Here are just some examples of ActionScript 3 classes:
MovieClip class
Sound class
TextField class

So from the MovieClip class, we can create MovieClip objects. From the Sound class, we can create Sound objects, and so on.

But what exactly are these objects then?
In programming, these objects are the different elements that make up the parts of the application that you're building. In the same way that a car has different parts - an engine, a steering wheel, doors - applications are also made up of different parts. These parts are referred to as objects, and these objects are needed in order for your application to do what it's supposed to do.

When you're building an interactive Flash movie, for example, Sound objects are used so that your application can play audio. TextField objects are used so that your Flash movie can display text. And MovieClip objects are used so that you can add some animation. Each type of object has a specific role. Sound objects are for sound. And TextField objects are for text. You can't use a Sound object to display text, and you can't use a TextField object to play sound.

All these objects are important, and you'll need to create these different objects depending on what your application needs to do. A car without an engine would not run properly, in the same way that a Flash music player application, for example, won't run properly if it doesn't have any Sound objects.

So how do you create these objects using ActionScript 3?
In ActionScript 3, you can create objects by using the new keyword, followed by the class constructor. The class constructor is a special function that "constructs" or creates an object. The constructor function uses the same name as the class. So for example, the constructor function of the MovieClip class would be MovieClip(). The constructor function of the Sound class would be Sound(). So whatever the name of the class is the name of the constructor function as well.

So if we wanted to create a new TextField object:
new TextField();

Creating an object is referred to as creating an instance of the class. So in the example above, we just created an instance of the TextField class. But this is still incomplete. This instance must have a name. In most cases, we shouldn't have a nameless TextfField object. What if we had 10 of those? Then it would be terribly confusing to distinguish which one is which. Our objects need to have names so that we can effectively communicate with them and give them instructions through code.

So how do we give it a name then?
We can give our instance a name by assigning it to a variable, like so:
var myTextField:TextField = new TextField();

Using the var keyword, we begin the variable declaration. It is then followed by the author-defined variable name that we want to give the instance. So in this example, the variable name is myTextField. We then follow it up with the data type. In this example, the data type is TextField. The data type you use would be whatever the class name is.

So the syntax for creating an object would be:
var variableName:DataType = new ClassConstructor();


In the example below, 4 new objects have been created:

We won't go into detail as to how these objects can be used. For now, I just want you to know how to create them. In future lessons, we'll be taking a look at some of the the most commonly-used ActionScript 3 classes in more detail.

Symbol instances are also objects
Did you know that when you convert something into a button or movie clip symbol that you're actually creating an object as well? Yes! In Flash, buttons are actually instances of a class called the SimpleButton class, while movie clips are instances of the MoveClip class. This would be referred to as creating symbols at authoring time, because we create the symbols while we are "authoring" or making the Flash movie. If you create an object using code, then that would be referred to as creating an object at runtime, because the object gets created only when the Flash movie itself starts running.

1 comment: