×
Namespaces

Variants
Actions
Revision as of 12:29, 29 March 2010 by manikantan (Talk | contribs)

Archived:How to implement alert screens using exceptions in ActionScript 2.0

From Nokia Developer Wiki
Jump to: navigation, search


Contents

Introduction

When we are designing an application, we always need alert screens to get the user attention or show an error that occurred. Most cases we code these alerts and its actions in the main application, which causes an ugly and a hard understanding code in the final result. To solve this problem I propose creating a class that extends from Error object (represents the exception object in ActionScript 2.0) with the MovieClips and Buttons objects inside this class; at last to use it you only need throw an exception in your main application.

Step 1: Creating your alert screen.

The aim of this step is creating your own visual alert screen. Here I'm going to show you how to make a basic one, but it can be easily modified to increase functionalities.

1.1 - First you need to create a MovieClip with a 50% transparency with name alert_screen_mc;

Note.pngNote: Remember to export the alert_screen_mc MovieClip for the ActionScript.

1.2 - Put a 100% transparent button to block the mouse events;

1.3 - Create another MovieClip with name alert_box_mc with the following objects inside:

  • A text field with instance name text_field;
  • A button with name ok_btn.

Your alert should looks like with the shown below:

AlertScreen.PNG

Step 2: Creating your own exception class

It's easy creating and extending an Error class. You only need use the word [extends] as shown below:

class MyOwnException extends Error {
 
private var message:String; //message to show on the alert box.
 
public function MyOwnException(message:String, alert_type:String) {
this.message = message;
// Create and attach the alert to your stage.
var target = _root.attachMovie(alert_type, "current_shown_alert", 1000, 0, 0);
// Insert the message in the text field at the alert box.
target.alert_box_mc.text_field.text = message;
// Remove the alert from your stage when clicking the button.
target.alert_box_mc.ok_btn.onRelease = function() {
removeMovieClip(target);
};
}
 
}

Your constructor receives two parameters: the message that you want to be shown and your alert screen name (created in the step 1).

Step 3: Using your exception

Ok. This is the easier step of all; only need throw your recently created exception and then you'll can see your alert on the screen. When you click the ok button the alert will disapear.

Example throwing an exception to show the alert screen:

throw new MyOwnException("My exception message.", "alert_screen_mc");

Note.pngNote: Just start the appplication and the alert will be shown.

Source Code for Download

File:AlertScreenUsingExceptions.zip

89 page views in the last 30 days.