I was going to comment this just looks like a type problem. Seems IC got to it first and gave the correct code.
for the most part, you are correct, however what you're really doing is defining the variable "Type".
Lets say Water is a subclass of liquid.
I can make two different variables for water.
- Code: Select All Code
Water w = new Water();
Liquid l = new Water();
The difference between these two variables, is that i can't directly call anything new in water.
For instance lets say i have a public method called getShiny(); & variable called "int Shiny =1;" in water but not liquid. if you use the w variable you can use the method getShiny but with the l variable it will throw a error.
However lets say in liquid we have a method called getColor(); and variable color. In water, we change this method to always return blue no matter what the color really is. We can still call this method getColor(); from the l variable and it will act the same way as the w variable.
EDIT. Seems this doesn't apply to action script. Never mind, the replacement code is in the next post.
Constructors run when a class is created. When you say "new object()" you are running a constructor. Every class is compiled with a constructor whether you make it or not. The compiler will automatically give it a default one; and this is perfectly fine if you don't need a constructor. In java a constructor is the class name with a parameter list. in flash this looks like a constructor to me.
- Code: Select All Code
class Enemy{
//ignore these variables right now.
var image: MovieClip;
var hp: int;
var lust: int;
var fave: MovieClip:
function Enemy(image : MovieClip, hp : int, lust : int, fave : MovieClip){
this.image = image;
this.hp = hp;
this.lust = lust;
this.fave = fave;
}
}
however lets say we know some standards about Enemy, like the fact every enemy starts at 100HP and lust is 0. so i'm going to make a second constructor.
- Code: Select All Code
class Enemy{
//ignore these variables right now.
var image: MovieClip;
var hp: int;
var lust: int;
var fave: MovieClip:
function Enemy(image : MovieClip, hp : int, lust : int, fave : MovieClip){
this.image = image;
this.hp = hp;
this.lust = lust;
this.fave = fave;
}
//notice no HP or lust variable?
function Enemy(image : MovieClip, fave : MovieClip){
this(image, 100, 0, fave);//this calls the constructor above but gives it these values.
}
}
in my example you can now say "new Enemy(image, fave);" or "new Enemy(Image, hp,lust,fave);" to create the new object.