How to use interface in ActionScript 3

There are a lot of good ActionScript 3 books out there, but in most of them all the examples about inheritance, composition, interface, polymorphism and more complex object oriented principles are done using data classes. It’s good to explain the principles, but when you are ready to code some actual classes in Flash, some problems start to arise. Most of my problems come from the fact that 90% of the time I am coding classes that are a visual representation of something. So these classes extends the Sprite or MovieClip class.

So I was coding this game class where I have multiple kind of monsters but I want to use the same functions without checking what kind of monster it is. This is a case where polymorphism comes in handy. The thing is I didn’t do my classes of monster using inheritance (which maybe I should have done), so in order to use polymorphism, I have to create an interface with the public methods common to all my monster classes. So I open the Colin Moock book and start looking at how to code interfaces. Here is a list of quick facts that will help you while doing so.

You have to list all public methods that will be common for the classes that implements this interface;

You do so by writing the definition of the function, its name, its parameters and its return type;

You don’t have to open and close curly braces after the method definition

You don’t specify if the method is private, public or protected (that’s kind of obvious but I did that mistake)

You use interfaces and polymorphism when you want to code in a way that is more maintainable. If the flash compiler is set to soft (it won’t complain for type error) then you don’t have to bother with that.

But if your code is going to be used for more than one project or by more than one person, I would advise using this kind of technique. Having your compiler in strict mode is really good for catching error before they arise.

So in my case, I wanted to check if I could shoot this monster or hurt it. I had to tell the compiler which monster it was in order to use the getShoot method. In a complex game, it could have been one type of monster from a dozen type and I would have to check the current monster against each type. Instead I make sure that every monster implements the IMonster interface and because of that the compiler will throw me an error if I don’t code a getShoot and canGetShoot method with the same definition as in the interface (same parameters and return type).

When I will shoot a monster, I only have to cast any monster to the IMonster type and I am sure it will have the previously mentionned methods. In time it will save me trouble, which might not be obvious at first.

Regarding casting a spriteMouton to Displayobject.
The IMonster interface could be extended to the interface of the DisplayObject and thereby ensuring that classes implementing the IMonster always must extend DisplayObject hence no need to type cast. Just a thought.

@Paul
Yes and No, if what you are looking for is polymorphism then you probably want to cast your object to an interface.

Now you could just do what you say and later push myMonster into an array and when retrieving it, cast it to the interface and that would work just as good. It all depends of the context of your application.

@post 10
I think that You never will do a cast in the Paul code.
You can use myMonster as MovieClip or implementation of a IMonster interface without casting in every method that receive as parameters or an MovieClip or an IMonster.

I’m a newbee from the point of view of AS3, but I work with PHP5, C++, Java and so on and that is the main rule ( I suppose so ). am I in fault for AS3?

Interace is useful when dealing with shared asset libraries as well. You may refer to the interface class in the application for general definitions of what implementing class will define. Much like intrinsic did for AS2.

this doesn’t work because you can’t call the displayObject specific parameters like x, y, addEventListener and so on. There has to be another way to make movieclips subject to an interface without losing it’s displayObject specific methods and params. Anyone?

You don’t need to say variables and methods are public in an interface because it is implicit. An interface is a minimum list of public variables or methods that a class must have in order to implement it.

I’m very confused with interfaces because i do a lot of elearning and i’ve never seen an example similar to what i do. Can you please give me an example of using interfaces in elearning CBTs? I make a lot of templates and they all have data loaded from xml and back, next, and help buttons and a review quiz at the end. Then a results screen telling you what you missed(your score) and what the right answer was and it’s printable. Thanks!

An interface is an abstract class it cannot be instantiated. That means you cannot do this : im:IMonster=new IMonster();. To find an interface useful you have to understand polymorphism.A simple definition of Ploymorphism is from Generalisation to specialisation.Ex: You have fruits you want to know how many vitamins they contain and display it. Here is some code in ActionScript:
//(Generalisation)
public interface Ifruit
{
function getVitamins():string
}
//(Specialisation)
public Class Apple implements Ifruit
{
private number_of_vitamins_:string;
//some code
function getVitamins ():string
{
return number_of_vitamins;
}
};

and what’s the difference between making an Interface, and extending class from a Fruit class i.e. like in the example above, but to pass to the:
function DisplayFruitVitamins(fruit:Fruit):void
{
displayedtext_.Text=fruit.getVitamins ():
}

@northmantif
An interface is a signature, while if you extend from a Fruit class you must actually implement the fruit class. How would you implement the getVitamins() function for a Fruit? You can’t, because generic fruits don’t have vitamins. A generic fruit is just a generalization, not actually real.

So interfaces are good for when you need to generalize something that is in common with multiple real things, and you want to create a binding signature for those things.

For those struggling about the fact that once an interface is implemented you lose for example the displayobject inheritance (because you cannot extend your interface to a class,) .. the solution is the other way around… you shouldn’t cast to a displayobject but have your object already as displayobject and then eventually cast to your interface when you need specifc stuff from there (isn’t that the meaning of an interface.. ). If I have a base class Page (which may or may not implement an interface) but for sure will extend a displayobject and other subpages like Homepage, Contactpage and so on which implement an interface.. when I create an Homepage instance I create it as a Page(displayobject) so I can move it, attach listerners and add it to stage.. only when I need to call a specific method from the interface I cast it to the interface and for example call an initPage() …

@ yohami
In addition, this will allow your code to be dynamic down the road. Think of an interface as a way of building functions that can be variable within a class. If you create an interface, then you can create several classes that implement that interface. In this way, those classes define a behavior (functionality) described in the interface. Then you can create an instance of these classes within your main object, and change it over time to basic render all the functions within the interface as ‘variable’ functions.

The short and easy description is that using interfaces allows you to add dynamic behavior (functionality) to your classes, as opposed to hard coding it.

So then I can just have an array of Entity objects in my main game driver class. You can easily perform operations on all objects at once by calling them all Entities, then any specific code will filter through their Settings object. If it is Melee dps monster or a ranged caster monster, or a tall platform, short platform with spikes, w/e.