Game Development Reference
In-Depth Information
public interface IEventDispatcher {
function addEventListener(type:String, listener:Function,
useCapture:Boolean=false, priority:int=0,useWeakReference:
Boolean = false):void;
function removeEventListener(type:String, listener:Function,
useCapture:Boolean=false):void;
function dispatchEvent(event:Event):Boolean;
function hasEventListener(type:String):Boolean;
function willTrigger(type:String):Boolean;
}
Note the differences between an interface and a class. Interfaces
are always public or internal, just like their class counterparts, but
none of the methods have any attributes because they are all
assumed to be public. Interfaces cannot include variables, but they
can include getter and setter methods, which can substitute for
variables.
At this point, you might very well be asking,
Why would I ever
bother to use an interface when I can simply extend a class to
make sure all the subclasses have the available methods?
The
answer is that unlike some other languages, classes in Flash cannot
inherit from multiple superclasses. This poses a problem when you
need to extend one class but include functionality from another
class in a different inheritance hierarchy.
A good example of a situation like this is the IBitmapDrawable
interface that is part of the Flash display package. When you want
to draw something to a BitmapData object, you can use either
another BitmapData object, or a DisplayObject. In order to keep
just any object from being passed to the draw method, both
BitmapData and DisplayObject implement an interface called IBit-
mapDrawable. This interface actually doesn
t do anything but
enforce this compatibility between two classes that have nothing to
do with each other. The draw method can then look like the
following:
'
public function draw(source:IBitmapDrawable, matrix:Matrix = null,
colorTransform:ColorTransform = null, blendMode:String = null,
clipRect:Rectangle = null, smoothing:Boolean = false):void
When an object is passed for the source parameter, Flash checks
to see if the object implements the IBitmapDrawable interface and
can throw an error to let the developer know. Here is another
example of a class implementing an interface while extending an
unrelated class.
package {
import flash.events.IEventDispatcher;
import flash.events.EventDispatcher;
Search Nedrilad ::




Custom Search