31 October 2011
Object-oriented programming (OOP) is a style of programming that focuses on using objects to design and build applications. Think of an object as a model of the concepts, processes, or things in the real world that are meaningful to your application. For example, in a project management application, you would have a status object, a cost object, and a client object among others. These objects would work together (and with many other objects) to provide the functionality that you want your project management application to have.
Object-oriented programming is used to develop many applications—simple and complex applications, business applications and games, mobile and desktop applications. Developers choose to program in the object-oriented paradigm because the proper use of objects makes it easier to build, maintain, and upgrade an application. Also, developing an application with objects that have been tested increases the reliability of the application.
ActionScript 3 is an object-oriented language. Almost everything you will need to build an application using ActionScript 3 is an object—Sprite, MovieClip, TextField, Array, and the like. Understanding objects and knowing how to work with them is key to developing any application using ActionScript 3. This article is the first of many that will focus on object-oriented programming concepts within ActionScript 3. In it, you will learn what an object is, what a class is, how to instantiate an object, and how to work with objects using dot notation.
In programming terms, an object is a self-contained component that contains properties and methods needed to make a certain type of data useful. An object’s properties are what it knows and its methods are what it can do. The project management application mentioned above had a status object, a cost object, and a client object, among others. One property of the status object would be the current status of the project. The status object would have a method that could update that status. The client object’s properties would include all of the important details about the client and its methods would be able to change them. The cost object would have methods necessary to calculate the project’s cost based on hours worked, hourly rate, materials cost, and fees.
In addition to providing the functionality of the application, methods ensure that an object’s data is used appropriately by running checks for the specific type of data being used. They also allow for the actual implementation of tasks to be hidden and for particular operations to be standardized across different types of objects. You will learn more about these important capabilities in Object-oriented concepts: Encapsulation.
Objects are the fundamental building blocks of applications from an object-oriented perspective. You will use many objects of many different types in any application you develop. Each different type of object comes from a specific class of that type.
A class is a blueprint or template or set of instructions to build a specific type of object. Every object is built from a class. Each class should be designed and programmed to accomplish one, and only one, thing. (You'll learn more about the Single Responsibility Principle in Object-oriented programming concepts: Writing classes.) Because each class is designed to have only a single responsibility, many classes are used to build an entire application.
An instance is a specific object built from a specific class. It is assigned to a reference variable that is used to access all of the instance's properties and methods. When you make a new instance the process is called instantiation and is typically done using the
Think about classes, instances, and instantiation like baking a cake. A class is like a recipe for chocolate cake. The recipe itself is not a cake. You can't eat the recipe (or at least wouldn't want to). If you correctly do what the recipe tells you to do (instantiate it) then you have an edible cake. That edible cake is an instance of the chocolate cake class.
You can bake as many cakes as you would like using the same chocolate cake recipe. Likewise, you can instantiate as many instances of a class as you would like. Pretend you are baking three cakes for three friends who all have the same birthday but are different ages. You will need some way to keep track of which cake is for which friend so you can put on the correct number of candles. A simple solution is to write each friend's name on the cake. Reference variables work in a similar fashion. A reference variable provides a unique name for each instance of a class. In order to work with a particular instance, you use the reference variable it is assigned to.
Just as a cookbook contains numerous recipes, ActionScript 3 includes a number of classes that you can use. A very commonly used class in ActionScript 3 is
Sprite object can display graphics and contain children (learn about display objects in ActionScript 3 fundamentals: Display objects and the display list - available soon). You will be instantiating many Sprites while developing in ActionScript 3. Instantiation is actually a two-step process. First, you declare a reference variable for the object:
public var myFirstObject:Sprite;
Then you create the new object—using the keyword
new—and assign it to the reference variable.
myFirstObject = new Sprite();
You can either declare a reference variable and assign a newly instantiated object into it in separate statements:
public var anotherObject:Sprite; … anotherObject = new Sprite();
or in the same statement:
public var myFirstObject:Sprite = new Sprite();
It’s simple to instantiate the same class numerous times. The code below creates four instances of the Sprite class. Each instance is assigned to its own reference variable.
public var firstSprite:Sprite = new Sprite(); public var secondSprite:Sprite = new Sprite(); public var thirdSprite:Sprite = new Sprite(); public var fourthSprite:Sprite = new Sprite();
One you have instantiated an object, use its reference variable and dot notation to access its methods and properties.
In ActionScript 3 (like most object-oriented languages), dot notation is used to indicate that a method or property belongs to or is contained within a particular class. Use the reference variable you declared followed by a dot to access the instance’s properties and methods.
In the following code, a reference variable named
myFirstObject is declared. The
Sprite class is instantiated and the instance is assigned to
myFirstObject. Then, using that reference variable and dot notation, values are assigned to the x and
visible properties of the instance, and the methods
stopDrag are called.
public var myFirstObject:Sprite = new Sprite(); myFirstObject.x = 300; myFirstObject.visible = true; myFirstObject.startDrag(); myFirstObject.stopDrag();
When you instantiate multiple instances of the same class, each instance is assigned to a reference variable. Using the reference variable with dot notation allows you to refer to only one instance at a time. Therefore, the changes you make to one instance will not affect any other instance.
The code below shows multiple instances of the
Sprite class being created and assigned to a reference variable. Each instance is then accessed using its reference variable to set properties or call methods.
public var firstSprite:Sprite = new Sprite(); public var secondSprite:Sprite = new Sprite(); public var thirdSprite:Sprite = new Sprite(); public var fourthSprite:Sprite = new Sprite(); … firstSprite.x = 100; thirdSprite.stopDrag(); fourthSprite.visible = false; secondSprite.startDrag();
Sometimes a class contains another class. Keep using the dots to indicate containment. For example,
beginFill() drawCircle() and
endFill() are all methods of the
graphics class that is inside the
public var spriteInstance:Sprite = new Sprite(); spriteInstance.graphics.lineStyle(3); //width of line spriteInstance.graphics.beginFill(0xff0000); //RGB color spriteInstance.graphics.drawCircle(100,100,50); //x,y,radius spriteInstance.graphics.endFill();
Display objects will be some of the objects you work with most in an ActionScript 3 project. Learn about them in ActionScript 3 fundamentals: Display objects and the display list (coming soon). You should also learn how to create your own objects by learning how to write classes. If you want to continue learning more about object-oriented programming concepts in ActionScript 3, read about encapsulation and composition.