3

I have an Entity class with a destroy() function.

I also have an Enemy class that extends Entity, and I want to add some lines to the destroy() function.

Is there a way to extend functions in ActionScript 3, or is copy and paste the way to go? Thanks.

Marty
  • 39,033
  • 19
  • 93
  • 162
apscience
  • 7,033
  • 11
  • 55
  • 89

1 Answers1

9

You need to mark the method with the override keyword, and from there use the same namespace (public, protected, etc) and name that make up the method you want to override in the class you're extending.

The method must also have the same return type and accept the same arguments

Sample override:

override public function destroy():void
{
    // add more code

    super.destroy();
}

If you exclude the line which reads super.destroy(), the function within the base class will not be run, and only your new code will be used instead.

Marty
  • 39,033
  • 19
  • 93
  • 162
  • Thanks! I thought super() could only be used for constructors. – apscience Oct 07 '11 at 00:06
  • Super is just a reference to the base class being extended and generally whenever you override a function you should always call super.funcName() Sometimes it is not needed but it is good practice. – The_asMan Oct 07 '11 at 01:42
  • It's good because you can place the statement wherever you like in your overridden function, meaning you can decide whether you need to run the code in the extended class before your new code, after it or anywhere inbetween. – Marty Oct 07 '11 at 01:49
  • 1
    `Super` answer! Sorry, I couldn't resist. – Joel Feb 11 '13 at 19:08