6 min read

This article covers the following:

  • Introduction to classes and object-oriented programming
  • Working with the document class/main application file

Introduction to classes and object-oriented programming

In this article we will learn to write our own classes that can be used in Flash, along with Flex Builder and Flash Builder. If you’re a developer using the Flash IDE, it might be the first time you’ll write your own classes. Don’t worry about the difficulty level if you are new to classes. Learning how to program  in an OOP way using Papervision3D is a good way to become more familiar with classes and it  might motivate you to learn more about this subject. You will not be the first who learned to program in an OOP way, as a side effect of learning an external library such as Papervision3D is.

So what are classes? In fact, they are nothing more than a set of functions (methods) and variables (properties) grouped together in a single file, which is known as the class definition. A class forms the blueprint for new objects that you create.

Sounds a bit vague? What if you were told that you’ve probably already used classes? Each object you create from the Flash API is based on classes. For example, Sprites, MovieClips, and TextFields are objects that you have probably used in your code before. In fact, these objects are classes. The blueprints for these objects and their classes are already incorporated in Flash. First, have a look at how you can use them to create a new Sprite object:

var mySprite:Sprite = new Sprite();

Looks familiar—right? By doing this, you create a new copy of the Sprite class as an object called mySprite. This is called instantiation of an object. There’s no difference between instantiating built-in classes or instantiating custom written classes. Papervision3D is a set of custom classes.

var myObject3D:DisplayObject3D = new DisplayObject3D();

So, although you know how to use classes, creating your own classes might be new to you.

Creating a custom class

An ActionScript class is basically a text-based file with an .as extension stored somewhere on your computer, containing ActionScript code. This code works as the previously mentioned blueprint for an object. Let’s see what that blueprint looks like:

package {
public class ExampleClass
{
public var myName:String = "Paul";
public function ExampleClass()
{
}
public function returnMyName():String
{
return "My name is" + myName;
}
}
}

On the first line, you’ll find the package statement, followed by an opening curly bracket and ended with a closing curly bracket at the bottom of the class. Packages are a way to group classes together and represent the folder in which you saved the file. Imagine you have created a folder called myPackage inside the same folder where you’ve saved an FLA or inside a defined source folder. In order to have access to the folder and its classes, you will need to define the package using the folder’s name as shown next:

package myPackage {
...
}

This works the same way for subfolders. Let’s imagine a folder called subPackage has been added to the imaginary folder myPackage. The package definition for classes inside this subfolder should then look like this:

package myPackage.subPackage {
...
}

If you don’t create a special folder to group your classes, you can use the so-called default package instead of defining a name. All the examples in this article will use default packages. However, for real projects it’s good practice to set up a structure in order to organize your files.

After the package definition, you’ll find the class definition, which looks as follows:

public class ExampleClass
{
...
}

The name of the class must be the same name as the class file. In this example, the file needs to be saved as ExampleClass.as.

Besides the fact that working packages is a good way to organize your files, they can also be used to uniquely identify each class in a project.

At the top of the class definition you’ll see the word public, which is a keyword defining that the class is accessible to all other code in the project. This keyword is called, an access modifier.

The defined name of the class will be used to instantiate new copies of the class. Instantiating this class could be done like this:

var classExample:ExampleClass = new ExampleClass();

Inside the class, a string variable is defined in pretty much the same way as you would when working with timeline scripting.

public var myName:String = "Paul";

The definition of a variable inside a class is called as a class property. You can add as many properties to a class as you want. Each definition starts off with an access modifier. In this case the access modifier is set to public, meaning that it is both readable and writeable by code located outside the class:

var classExample:ExampleClass = new ExampleClass();
classExample.myName = "Jeff";

As you can see, this creates an instance of ExampleClass. We also changed the myName property from Paul to Jeff. When a property is defined as public, this is allowed to happen. In case you want access to this property inside the class itself, you can mark the property as private:

private var myName:String = "Paul";

Executing the previous code to change myName from Paul to Jeff will result in a compile-time error. In the next lines we see the creation of a new function called ExampleClass in the class. Functions that have the same name as the name of the class are known as constructors.

public function ExampleClass()
{
}

Constructors always have a public access modifier and are called automatically each time the class is instantiated. This means that the code inside this function will be executed automatically.

All other function definitions inside the class are called methods.

public function returnMyName():String
{
return "My name is" + myName;
}

At the end of this method definition, you’ll notice a colon followed by a data type. This defines the type of object the method returns. When you work with functions on the timeline in Flash, you can define this as well, but it’s not required to do so. The method returnMyName() is defined to return a string. In case you do not want to return any data type, you can define this by using a void as the return type:

public function returnNothing():void
{
//Do not return something
}

We need to define return type only for methods and not for constructors.

Classes, as well as properties and methods, have access modifiers that define from where each of these objects can be accessed. So far we’ve seen that a public keyword allows code outside the class to access a property, or call a method inside the class. When you allow access from other code, you need to be aware that this code can mess up your class. You can prevent this by using the private keyword, which makes the property or method only accessible inside your class. Two other access modifiers that are often used are internal and protected. Classes inside the same package can access internal methods or properties and protected methods can only be used inside a related subclass. Subclasses are a part of inheritance, which will be explained in a bit.

As long as you have not planned to give access to scripts outside your class, it’s a good practice to mark all properties and methods of a class as private by default. When defining a property or method, you should always ask yourself whether you want them to be accessed from outside your class.


Subscribe to the weekly Packt Hub newsletter

* indicates required

LEAVE A REPLY

Please enter your comment!
Please enter your name here