I need a little help understanding how classes work in Actionscript 3. I understand you start with “package” and why and then go to import any necessary libraries, as well as then naming the class and stating if it’s public/private and extends anything.
After that is what I don’t understand. It seems you write “(public) function class name()
I don’t understand why you do this and what goes in the curly brackets.
I’ve probably missed a bit of earlier reading because I’ve done a little reading but I can’t seem to get it.
Could someone try explain it to me? Thanks.
ActionScript 3 Classes
The
packagestatement.Okay, so firstly like you mentioned, a class must be wrapped by a
package1. This gives us the first block, where you need to define the class.The
packagestatement reflects the location of the class relative to the.fla2. For example, if you have a folder “classes” within the same directory as the project .fla, then classes within that folder will need a package statement that reflects that:Defining the class.
Within a package statement, you may insert one class. Do not confuse this with the package itself, which can contain many classes – each class just needs to have its own file with the same package statement.
A class definition is made up of up to 5 parts:
internalorpublic. Aninternalclass can only be seen by classes within the same package, whereaspublicclasses can be seen from anywhere in the project.If you wanted to create a class called “Person” within the package
classes, then we would end up with:Properties.
Classes can contain properties. Properties are defined using the
varkeyword. They may belong to one of a number of namespaces (including your own) and are used to hold values that belong to your class. Properties are most commonly found clustered together at the top of your class.Our
Personclass may enjoy the propertiesheightandweight:These properties can be accessed via any instance of
Personthat you create. Each instance will have its own set of these properties.Class constructors (I believe this is what you’re asking about).
Constructors are used to hold logic that should be run as soon as an instance of your class is created. The class constructor has the same name as the class itself. It must be
publicand it does not return anything. Constructors can accept arguments, which are typically used to pass in references to dependencies for that class or required values.Methods.
Methods are used to hold logic that can be run when calling that method. Methods often return values and can accept arguments. Methods can belong to any namespace that you would expect properties to be able to belong to.
We may want to be able to easily determine the BMI of each instance of
Personthat we create, so we should create a method for that:Instances.
Now that we’ve defined our new class, we can create instances of this class using the
newkeyword. This can be done from anywhere that can access thePersonclass, which in this case is anywhere in the project because we’ve made the classpublic.Though the class is
public, accessing it from anywhere outside of the package it belongs in will require the use of animportstatement. This statement will need to be used within any class that belongs to a different package. Theimportstatement follows the same name used for thepackageand includes the name of the class you want to include on the end:Once you’ve imported
Person, you can create instances of it and assign them to a variable with differentheightandweightvalues:We can then obtain the BMI for each person using their
getBMI()method:1 You can place classes outside of a package which can be referred to in the same .as file.
2 You can add more source paths, and packages can be relative to that.