var ASMain:TutorialBank = new TutorialBank();
INTRODUCTION
this tutorial will cover the basics and intermediate levels of OOP class writing. this thread can be considered a disambiguation of inglors AS OOP thread. some assumed knowledge includes variables, functions, basic OOP (prototyping), and it is recommended that you understand class instantiation.
TERMINOLOGY
property - a property is what one would usually refer to as a variable outside of classes. a class stores certain pieces of information, known as properties, as either static or dynamic information. an example of a property would be the MovieClip._x (which is the _x property of the MovieClip class).
method - again, a method is what one would usually refer to as a function outside of classes. a class performs normal actions through these methods. an example of a method would the Math.sin() (which is a sin method of the Math class).
AS: OOP (Object Oriented Programming) by Inglor
AS: Prototype, Proto & Inheritance by BleeBlap
CLASS SYNTAX
for actionscripted OOP classes, the file name must match the name of the class. for instance, if you wanted to create a class called MyFirstClass, the actionscript file must be named MyFirstClass.as (this name is case-sensitive, so capitalization does matter). to use the AS class, you must include the *.as file in the same directory as the FLA in which you intend to use it.
okay, begin by writing out the class delimiters:
class MyFirstClass{
}
PROPERTIES
accesstype var propName:DataType = new DataType();
first thing we want to do is initialize every property we intend to use in the class. any included variable must be given an access-type. the most common are "public" and "private". the public properties can be viewed and changed by any piece of code anywhere at anytime. some examples of public properties would be the _x and _y properties of the MovieClip class.
a private property can only be accessed and modified from a method within the class. an example of this would be the _depth property of the MovieClip class. you can change this with a public method, a process called "encapsulation" which i will cover later, but you cannot change _depth on its own.
so let us decide some properties to add to our class:
public var canTrace:Boolean = true;
public var allCaps:Boolean = false;
private var lastMessage:String = new String();
private var timesSpoken:Number = 0;
METHODS
accesstype function methdName(params:DataType):ReturnType{
}
think of these as glorified functions that are attached to each class and can modify properties. the access type works in the same way as with the properties. a public method can be accessed from any place in any code accompanying the class. an example of a public method is simple. any method youve ever used (swapDepths(), setTransform(), getVolume(), etc).
a private method is only able to be accessed by other methods in the function, and are commonly used just for assisting private methods. these methods essentially are only to be used to streamline reused code.
lets throw in a sample method to this class:
public function sendMessage(msg:String):Void{
if (!canTrace) return;
var curMessage:String = allCaps ? msg.toUpperCase() : msg;
trace(curMessage);
lastMessage = curMessage;
timesSpoken++;
}
CONSTRUCTOR METHOD
the constructor is the method that is called immediately when the class is instantiated. the requirements are that the constructor have the EXACT same name as the class itself, must have a public access-type, and there must not be a return type on the function. parameters may be included, but the same number of parameters must be included in the construction itself (explained later). a constructor is commonly used to set specific property values to use in the beginning. a normal constructor might look something like the following:
public function MyFirstClass(startTrace:Boolean, startCaps:Boolean):Void{
allCaps = startCaps;
canTrace = startTrace;
}