Handling the Alert Component in a Class

March 10, 2004

NOTE: The new mx.utils.Delegate class included with the Flash 7.2 updater provides a better solution for this problem. (9/16/2004)

The show() method of Flash MX 2004 Pro’s Alert component accepts a clickHandler parameter which may be a function or an object to handle the click events broadcast when the buttons are clicked. However, when used within a class as opposed within a frame as the example shows, the scope to a referenced function is lost. Instead, a listener object which has a click method and a reference target back to its parent is used.


// method within a class
private function onMyTask():Void {
 var t = this;
 var clickListener:Object = {
  click:function (evt) {
   if (evt.detail == 1) {
    this.target.onConfirmMyTask();
   }
  },
  target:t
 };
 var alertBox_ab = mx.controls.Alert;
 alertBox_ab.show("Are you sure you wish to do this task?", "Confirm Task", mx.controls.Alert.YES | mx.controls.Alert.NO, null, clickListener);
}
// Called when the "Confirm Task" alert box is confirmed
private function onConfirmMyTask():Void {
 // do what you need to do upon confirmation
}

This way, the listener object carries the scope with it to the alert box and can reference back to the parent class to complete the action in another function.

Posted in Flash/ActionScript

Comments

What about technochedder?

Posted at 12:55 PM on March 10, 2004 by Some Guy

Technochedder could not handle the budgetgrater.

Posted at 11:18 PM on August 13, 2004 by AmG

Categories

Search the Archive

Monthly Archive

Recent Entries

Texas Toast™ Technologies