redconservatory
redconservatory

Reputation: 21924

Using a callback (instead of an event) in Actionscript 3.0

Can anyone provide an example of how to write a callback instead of using an event to communicate between two classes (objects) in Actionscript 3.0?

Upvotes: 1

Views: 5528

Answers (2)

Patrick
Patrick

Reputation: 15717

Just pass a function to another one as parameter to make your callback :

class A {
 function A(){
 }
 // function to be called when work is finished
 private function workDone():void {
  //...
 }
 public function foo():void {
  var b:B=new B();
  b.doWork(workDone); // pass the callback to the work function

  //can also be an anonymous function, etc..
  b.doWork(
   function():void{
    //....
   }
  );
 }
}

class B {
 function B(){
 }
 public function doWork(callback:Function):void{
   // do my work
   callback(); // call the callback function when necessary
 }
}

Upvotes: 6

Kricket
Kricket

Reputation: 4179

What do you mean? A callback is a function that is called in response to an event - in AS parlance, it's an event listener. If you just want classes to communicate, have one of them call a method on the other.

Upvotes: 0

Related Questions