Class
What is a class?
A class is a group of types of things.
I know, that was rather complex, but read it while picturing it. A classroom is a group of types of students.
If we separate the classroom into smaller classes, we may have one class for the smart kids and one class for the slower comprehending kids.
So, classes in ActionScript are the same thing. The ‘Number:’ class is a group that can have different types of numbers in it. A ‘String:’ class is a group that can have different types of strings in it. Again, an ‘Object:’ class is a group of types of objects.
So, when you hear someone say a… class, you know it is a group of certain types of things.
Some classes:
Math
Array
Date
String
Stage
Key
Mouse
Button
TextFormat
TextField
Video
Sound
MovieClip
Boolean
Classes have properties and methods, and some have events.
So, what is in a Math class?
Well, math stuff is in a math class.
Date stuff is in a date class, stage stuff is in a stage class, key stuff is in a key class(keyboard that is) etc.
Remember that we said a variable can play different types of roles? These types of roles come from the different types of classes. It is like the variable had to go to a number class in order to be able to play the part of a number, or to a String class in order to play that of a sting, etc.
CLASS CREATION EXAMPLES
(not for the faint of heart)
(that is why I didn‘t write it)
EXAMPLE1 (not mine)
------------------------------------------------------------
// Filename Plant.as
class Plant {
// Define property names and types
var leafType:String;
var bloomSeason:String;
// Following line is constructor
// because it has the same name as the class
function Plant(param_leafType:String, param_bloomSeason:String)
{
// Assign passed values to properties when new Plant object is created
this.leafType = param_leafType; this.bloomSeason = param_bloomSeason; }
// Create methods to return property values, because best practice recommends against directly referencing a property of a class
function getLeafType():String
{
return leafType;
}
function getBloomSeason():String
{
return bloomSeason; }
}
var pineTree:Plant = new Plant("Evergreen", "N/A");
// Confirm parameters were passed correctly
trace(pineTree.getLeafType());
trace(pineTree.getBloomSeason());
------------------------------------------------------------
EXAMPLE2
I want to look at the first example again.
// Filename Plant.as
class Plant
{
var leafType:String;
var bloomSeason:String;
function Plant(param_leafType:String, param_bloomSeason:String)
{
this.leafType = param_leafType; this.bloomSeason = param_bloomSeason;
}
function getLeafType():String
{
return leafType;
}
function getBloomSeason():String
{
return bloomSeason;
}
}
var pineTree:Plant = new Plant("Evergreen", "May");
trace(pineTree.getLeafType());
trace(pineTree.getBloomSeason());
//Evergreen
//May
So let’s look at it in parts:
// Filename Plant.as
This is a comment. Letting you know how you should name the .as file, if you save it as one, which you should.
class Plant {
So, you are creating a new class and you name it ‘Plant’ because you want types of plants to be in it. You have to put the class stuff in the braces (function).
var leafType:String;
var bloomSeason:String;
This is where you make the names of your properties. This is like Array.length or Array.push. Notice we have not given ‘leafType’ and ‘bloom Season’ a String value (no = sign).
function Plant(param_leafType:String, param_bloomSeason:String)
{
this.leafType = param_leafType; this.bloomSeason = param_bloomSeason;
}
This function sets the name of the ‘leafType’ to be whatever is put in the argument. The same is done for the ‘bloomSeason’.
function getLeafType():String
{
return leafType;
}
This function simply returns the ‘leafType’ String, which is whatever is put in the ‘param_leafType’ slot. And it returns it as a String.
function getBloomSeason():String
{
return bloomSeason;
}
}
This function does the same for the ‘bloomSeason’. The last brace is to close the class.
var pineTree:Plant = new Plant("Evergreen", "May");
This is to test to see if the class works. We made a variable named ‘pineTree.’ We sent it to Plant school. And our new plant named ‘pineTree’ can take two arguments(parameters).
trace(pineTree.getLeafType());
trace(pineTree.getBloomSeason());
This is to see if the methods work. Remember that methods are functions.
In side of you class you will have functions that will make your class function. These work the same as all other functions. You can give them arguments (also called parameters).
EXAMPLE3 (not mine)
// Filename ImageLoader.as
class ImageLoader extends MovieClip {
function ImageLoader(image:String, target_mc:MovieClip, init:Object)
{
var listenerObject:Object = new Object();
listenerObject.onLoadInit = function(target)
{ for (var i in init) { target[i] = init[i]; }
};
var JPEG_mcl:MovieClipLoader = new MovieClipLoader();
JPEG_mcl.addListener(listenerObject);
JPEG_mcl.loadClip(image, target_mc); }
}
The ‘extends’ part says that all the stuff that comes with the MovieClip class will be in this class, but this class is the MovieClip class, extended
From PA 3.0
------------------------------------------------------------
Strategies for designing a class The topic of object-oriented design is a complex one; entire careers have been devoted to the academic study and professional practice of this discipline. Nevertheless, here are a few suggested approaches that can help you get started.
1. Think about the role that the instances of this class will play in the application. Generally, objects serve one of these three roles:
■ Value object: These objects serve primarily as containers of data—that is, they likely have several properties and fewer methods (or sometimes no methods). They are generally code representations of clearly defined items, such as a Song class (representing a single real-world song) or Playlist class (representing a conceptual group of songs) in a music player application.
■ Display object: These are objects that actually appear on the screen. Examples include user-interface elements like a drop-down list or status readout, graphical elements like creatures in a video game, and so forth.
■ Application structure: These objects play a broad range of supporting roles in the logic or processing performed by applications. Examples include an object that performs certain calculations in a biology simulation; one that is responsible for synchronizing values between a dial control and a volume readout in a music player application; one that manages the rules in a video game; or one that loads a saved picture in a drawing application.
2. Decide the specific functionality that the class will need. The different types of functionality often become the methods of the class.
3. If the class is intended to serve as a value object, decide the data that the instances will include. These items are good candidates for properties.
4. Since your class is being designed specifically for your project, what’s most important is that you provide the functionality that your application needs. It might help to answer these questions for yourself:54 Getting started with ActionScript
■ What pieces of information will your application be storing, tracking, and manipulating? Deciding this helps you identify any value objects and properties you may want.
■ What sets of actions will need to be performed—for example, when the application first loads, when a particular button is clicked, when a movie stops playing, and so forth? These are good candidates for methods (or properties, if the “actions” just involve changing individual values).
■ For any given action, what information will the class need to know in order to perform that action? Those pieces of information become the parameters of the method.
■ As the application proceeds to do its work, what things will change in your class that other parts of your application will need to know about? These are good candidates for events.
5. If there is an existing object that is similar to the object you need, except that it’s lacking some additional functionality you want to add, consider creating a subclass (a class which builds on the functionality of an existing class, rather than defining all of its own functionality). For example, if you want to create a class that will be a visual object on the screen, you’ll want to use the behavior of one of the existing display objects (for example, Sprite or MovieClip) as a basis for your class. In that case, MovieClip (or Sprite) would be the base class, and your class would extend that class. For more information about creating a subclass, see “Inheritance” on page 162.
------------------------------------------------------------
Resources for some information listed can be found in the Resource.pdf file.