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)
- interface can only extend other interfaces
- a class can implement more than one interface
So here is an example of an interface in As3:
-
package com.mutantfarm.monster{
-
public interface IMonster {
-
function getShot(damage:uint):void;
-
function getCanShoot():Boolean;
-
}
-
}
Here is how my visual class implements this interface while also extending the MovieClip class:
-
public class spriteMouton extends MovieClip implements IMonster{
It is important that you put the extend keyword before the implement keyword.
So most of what I just wrote about was details about interfaces, but when you start coding, all these details are important to know.
January 2nd, 2008 at 5:36 am
Hi Zeh. How does this save time? all the methods in the interface have to be defined again on each class, right?
January 3rd, 2008 at 1:47 am
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.
January 3rd, 2008 at 4:26 pm
Hi dgelineau,
Thanks. I get it. I thought the interface methods would be inherited or utilized, but they are just a template to make sure a certain class meets the standards. I will probably use it too.
Cheers,
Yohami
March 23rd, 2008 at 11:48 pm
[…] I know I am late to the party. I can’t even estimate how many times I’ve read a definition of an Interface over the last year. It has been a concept that totally baffled me for a long time, then I sorta […]
April 16th, 2008 at 6:13 pm
[…] this year, I came to understand how to use interfaces in ActionScript 3. I used them in a particular case where polymorphism was needed but that was it since then. Last […]