April 5, 2008
WikiAdvanced OOP in Flash
Advanced OOP in Flash
Topics that are covered are:
- Organizing Classes
- Building from existing classes
- Dispatching events
- Dealing with scope issues
Packages
Packages are a way of organizing code. It enables you to manage your code better. Packages help you to avoid class name conflicts.
You must declare the class with the package name - called fully qualified class name
class package.ClassName {
}
The class file must exist within a corresponding directory. You must refer to the class using the fully-qualified class name. You can use an import statement to enable you to use the shorted reference to the class.
There are two ways to import:
import package.ClassName;
import.package.*; // that's going to import classes in the package but not in the subpackage inside the package.
Extending Classes
When a group of classes can be further abstracted, you can create a superclass and inherit some functionality from that class.
A subclass can extend a superclass in order to inherit the properties and methods so as not to have to redefine the common characteristics and functionaolity in each class.
Clarinet, Oboe, Saxophone, Bassoon
ReedInstrument
Violin, Viola, Cello, ContraBass
Stringed Instrument.
Super super class
Musical Instrument
We use the extends keyword to create a subclass that inherits from a superclass.
You can reference public and private properties and methods of the superclass from within the subclass.
If you want to create a custom implementation of a superclass method within a subclass, simply declare the method in the subclass as you would any other method.
If you want a subclass implementation of a method to call the superclass implementation, use the super reference to refer to the superclass.
A suepr keyword is a reference to the super class
public function someMethod():DataType {
super.someMethod();
// subclass implementation
}
Dispatching Events
Basic event modeller that is being used in UI components in Flash. How can we implement those in our custom classes? Dispatch events in order to have an object notify a listener when something has occured. You can also use the same functionality UI components utilize.
Use the mx.events.EventDispatcher mix-in class to add standart event dispatching capability to a class.
First thing you need to do: Declare addEventListener, removeEventListener and dispatchEvent as Function properties.
public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function;
this is important, you are going to able to reference those methods. Those are going to be added to your prototype class. It works because it is added to your classes without you being add them, inherit them etc. In order to avoid compiler errors you need to declare those.
Use the mx.events.EventDispatcher.initialize() method in the constructor.
mx.events.EventDispatcher.initialize(this);
Use the dispatchEvent() method to dispatch events to listeners.
dispatchEvent({type:"eventType", target:this});
BookCollection.as
class library.collections.BookCollection {
private var _aBooks:Array;
public var addEventListener:Function;
public var removeEventListener:Function;
private var dispatchEvent:Function;
public function get collection():Array {
return _aBooks;
}
function BookCollection() {
mx.events.EventDispatcher.initialize(this);
_aBooks = new Array();
}
}
CheckOutList.as
class library.collections.CheckOutList extends library.collections.BookCollection {
function CheckOutList() {
}
public function addItem(bkItem:Book):Void {
_aBooks.push(bkItem);
dispatchEvent({type: "update", target:this});
}
public function removeItemAt(nIndex:Number):Void {
_aBooks.splice(nIndex, 1);
dispatchEvent({type: "update", target:this});
}
}
actions panel in the fla.
var oViewListener:object = new Object();
initializeListener();
function initializeListener():Void {
oViewListener.update = function(oEvent:Object):Void {
trace(oEvent.type);
};
kutuphane.addEventListener("update", oViewListener);
clItems.addEventListener("update", oViewListener);
}
Scope Issues
When using instances of classes that use event handler methods such as XML or LoadVars, the code in the event handler method cannot correclty reference properties or methods of the parent class.
public function someMethod():Void {
_xmlProperty.onLoad = function(bSuccess:Boolean):Void{
// xml object is a method of our class
someOtherMethod();
};
}
public function someOtherMethod():Void {
// method definition
}
The event handler methods can reference local variables declared just prior to the event handler method definition.
public function someMethod():Void {
var sLocalVar:String = ""someValue";
_xmlProperty.onLoad = function(bSuccess:Boolean):Void {
trace(sLocalVar);
};
}
So what you can do is to reference the instance of the class;
public function someMethod():Void {
var oClass:Object = this;
_xmlProperty.onLoad = function(bSuccess:Boolean):Void {
oClass.someOtherMethod();
};
}
Continue Reading
Back to Archive