Child Overriding Empty Parent Function

Hi everyone!

I have this “Ennemies” class with variables and functions that are commons to every ennemy in my game. Then, each ennemy has its own class that “extends Ennemies”. In a specific class, I have an Array<Ennemies> that stock every ennemy in the current map.

The problem I’m facing right now is that from the “Array”, I need to access

public function hit(damage:Int):Void {}

that every ennemy has, but each ennemy has different code lines in that function.

Right now, what I’m doing is that the “Ennemies” class has an empty function that I override in each ennemy child.

It works very well, but I was wondering if this is a good approach.

Thanks!

Sure, that’s fine!

You may find (over time) that you end up with some common code

class Enemy {
    
    ...
    
    public function hit (damage:Int):Void {
        
        hitPoints -= damage;
        
        if (hitPoints < 0) showAnimation (DEATH);
        else showAnimation (HIT);
        
    }
    
}

If not, that’s fine too, but it’s very normal to define a function in a base class, then override and specialize the implementation in child classes :slight_smile: