I have a class, Pawn, which is the base class for all “characters” in the game with properties such as health, that can move and die or speak, etc. One class extending this is the “Boy” class. The “Boy” class is the player controlled character in this game.
In order to control character “states”, state classes are created. These have methods such as onEnterState, onStateTick and onLeaveState. A state might be used to represent crouching, walking, jumping, and will be send messages such as “keyPressed” “<-” to handle or ignore as it choses.
My problem is that my base “state” class has methods as follows:
function onEnter ( pawn:Pawn ) :void;
function onLeave ( pawn:Pawn ) :void;
function onGameTick ( pawn:Pawn ) :void;
function onAnimEnd ( pawn:Pawn ) :void;
As you can see this is actually an interface, but I did try base classes first until it became apparent that ActionScript3 doesn’t like abstract classes much.
So anyway, a class “BoyJumpState” would obviously need to extend/implement this class/interface to ensure that the public methods are there for interaction, however this is hindered by the fact that ActionScript will not allow me to extend with, say, this:
public override function onEnter ( boy:Boy) :void { trace("Hey"); }
This is rejected compile-time because it doesn’t match the blueprint (i.e it doesn’t accept a Pawn). However, I need it to accept a Boy because methods specific to that class need to be utilised in the method body!
I’m figuring my original interface is the problem, and that I should be specifying that the parameter should instead extend pawn and not actually need to be one, however I’m not seeing any way to implement this in ActionScript.
…or I could be completely on the wrong track, in which case advisement would be gratefully received!
I realise I could not have the inheritance going on, but the game I’m making is more of a practice in Object Orientated Programming as it’s more large-scale than what I’m used to. However I’ve moved out of my comfort language (Java) and I’m really beyond myself as to what to do here.
Any help would be appreciated, thanks.
You don’t need to override the overridden method’s signature to match a covariant type. If you need a specific type in a subclass’s method, you have to check/cast in the overridden method though.