Search code examples
flashactionscript-3overridingtimelinedispatchevent

Overriding in Flash IDE


I'm trying to override dispatchEvent(), with no success in the Flash IDE. Reviewing Adobe docs on the method, it should simply be:

override function name()

As demonstrated by Grant Skinner, this is commonly and easily performed:

override public function dispatchEvent(evt:Event):Boolean {
    // code here
}

And while a thorough google search will repeat this syntax successfully used in Class files, doing so inside timeline code, natively in the Flash IDE is proving impossible. The first issue being that the use of public throws the error 1114: The public attribute can only be used inside a package.

That was obvious, however upon removing that, running the following (on the first frame of a new .fla file):

override function dispatchEvent(evt:Event):Boolean {
    trace(evt.type);
    return super.dispatchEvent(evt);
}

results in this error: 1024: Overriding a function that is not marked for override.

Documentation on this error implies that I failed to prefix the identically named function with the override statement, but obviously that's a fallacy, and I'm left flabbergasted as to what the solution may be.

To be clear, we know a class file will work, but that's an irrelavant issue. How do we get override function dispatchEvent() working in timeline code?


Solution

  • You can only override class methods of the class you extend(the base class).
    This follows the rules on inheritance.
    When you extend a class you inherit all of its methods. Overriding a method allows you to restructure that method. Accessing a method and overriding it are 2 different things.

    public class SomethingSomething extends Event{
      public function SomethingSomething ( ):void{
      }
      override public function dispatchEvent(evt:Event):Boolean {
        trace(evt.type);
        return super.dispatchEvent(evt);
      }
    
    }