Using addeventlistener handle another class file

131 Views Asked by At

I know I can use the addEventListener method to handle one:

addEventListener(SFSEvent.CONNECTION, MyMethod)

as I would for handling a method in another class? Like...

addEventListener(SFSEvent.CONNECTION, Myclass.class)

or

addEventListener(SFSEvent.CONNECTION, MyClass.method)
1

There are 1 best solutions below

0
On

You may pass another function handler to a class

For example

Class A {

     public function A() {
          addEventListener(SFSEvent.CONNECTION, MyMethod);
     }

     private function _handler:Function;

     public function set handler(value:Function):void {
           _handler = value;
     }

     private function MyMethod(e:SFSEvent):void {

        if (_handler) {
            _handler.apply(null, someParam);
        }
     }

}

Then pass the target handler to A instance

var a:A = new A();
var b:Myclass = new Myclass();
a.handler = b.someMethod;

If the function is a static function, You may just do it like this

addEventListener(SFSEvent.CONNECTION, SomeClass.aStaticFunction);