The Armor Games website will be down for maintenance on Wednesday 5/18/2022
starting at 9:00 AM Pacific time. We apologize for the inconvenience.

Object Oriented Programming And Flash Pt.2

We’ve covered the basics of what a class is, so lets look at how one is put together.

A class is written in a separate file from your main .fla, with the .as filetype. For example, MyClass.as. The convention with class names is to use something called upper camelcase – writing the name with no spaces, but capitalising the first letter of each word, such as MyClass, TomIsCool or CodingIsFun. This is just a convention, so you don’t have to use it but it will help to make your code more readable, both to yourself and anyone else who reads it.

Inside the .as file, you write actionscript just as you would in the actions window in the Flash IDE. In face, Flash can edit and write .as files, although the editor leaves a lot to be desired. Alternatively you can use a normal text editor, or anything that can write to text files!

A class consists of a combination of three basic elements.

Firstly, you have properties. This is just the name you give a variable when it is stored on an instance of a class, and work exactly the same. Store strings, numbers, booleans and references to other objects with properties.

Secondly you have methods. This is just the name we give functions when they sit on an object and again, work exactly the same. Define parameters, process them and return a value.

Thirdly, we have getters and setters, which act a bit like a midpoint between properties and methods, and I’ll explain them further in a bit.

The convention with all three is to use lower camelcase, capitalising the first letters of each word except for the first, such as myProperty, anAwesomeMethod or totallyAmazingSetter.

Here is an example class, written in ActionScript 3 format (although the concepts are all applicable in AS2):

package
{
public class MyClass
{
private var myPrivateProperty:String;

public function get myProperty():String {return this.myPrivateProperty;}
public function set myProperty(myProp:String):void
{
if(myProp != “LOLCats”) myPrivateProperty = myProp;
}

public function MyClass()
{
this.myPrivateProperty = “AYB”;
}

public function addToProp(addProp:String):String
{
this.myPrivateProperty = this.myPrivateProperty + addProp;
return this.myPrivateProperty;
}
}
}

I’ll go through the concepts one by one.

First of all, ignore the package line if you don’t know what it is, that’s a lesson for another day.

Then we define the class with the name MyClass. Easy!

We define a private property called myPrivateProperty. Properties and methods can be defined as private, which means only code written inside this class can ‘see’ them and use them, or as public, meaning any code in any part of your programme can use them.

We then create a getter/setter pair for the property. The pair act together to make something similar to a property, in that you can use code like:

myClassInstance. myProperty = “Dancing Hamsters”;

which is an example of the setter being used (it is called whenever you set the value), or

var p:String = myClassInstance. myProperty;

which is the getter, used whenever you get the value. You might think it’d be easier to just make the property public and forget the getter/setter all together, and for a lot of cases you’d be right. But take a look inside the setter function. You can see that if you try to set the value to “LOLCats”, it won’t do anything. That’s the important thing about getters/setters – you get control over what is set and what is gotten. In any multi-class application, this is a vital tool in writing clean, bug-free code.

We then define a method with the same name as the class, MyClass. This is known as defining the constructor. This function is called automatically every time you create a new instance of your class, so you should put all your setup code in there, giving properties their initial values and such.

Finally, a regular method is defined, addToProp. This method takes a string for a parameter, adds it to myPrivateProperty and returns the new value of myPrivateProperty.

Next time I’ll cover the inheritance I mentioned before, its a pretty big topic!

Tom-

We may use cookies to help customize your experience, including performing analytics and serving ads.
Learn More