This example code for Actionscript 3.0 is the class I wrote titled MessageBoxManager. The description for this example is in this post: Flash Game Programming: Treating Game Messages Similarly to Particles with a Life Timer .
If you would like to see this class used in a small project, go here: Project Examples: SWF Illustrating the Star, Starfield, and MessageBoxManager Classes.
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class MessageBoxManager extends MovieClip {
//This variable keeps track of how long the message is displayed.
private var displayTimer: int;
//This variable keeps track of how long since the message was created.
private var timeSinceCreated: int;
//A text field container to hold the messages.
private var thisTextField:TextField;
//A TextFormat object to alter the appearance of the text as needed.
private var thisTextFormat:TextFormat;
//A Boolean variable to notate if the message should not dissapear in a few seconds.
private var permaDisplay: Boolean;
public function MessageBoxManager() {
//Constructor function to initialize the variables.
thisTextField = new TextField();
thisTextFormat = new TextFormat();
permaDisplay = false;
thisTextField.border = true;
thisTextField.borderColor = 0x00FF00;
thisTextField.width = 630;
thisTextField.height = 100;
thisTextField.alpha = 0;
thisTextField.textColor = 0x00FF00;
thisTextField.embedFonts = true;
displayTimer = 0;
thisTextField.x = 5;
thisTextField.y = 10;
thisTextField.alpha = 0;
//When this object is added to the stage by the parent class, call the function
//that draws the text box on the screen.
this.addEventListener(Event.ADDED_TO_STAGE, DrawMessageBox);
}
public function DrawMessageBox(event:Event){
//This object has been added to the stage by the parent.
//Remove the event listener.
this.removeEventListener(Event.ADDED_TO_STAGE, DrawMessageBox);
//Draw the message box on the stage.
parent.addChild(thisTextField);
}
public function addNewMessage(myMessage:String, size:int, font:String, seconds:int, alignment:String, permaDisplay:Boolean) {
//Add a new message to the text box.
//Set the display timer for the number of frames to show the message.
this.displayTimer = 60 * seconds;
this.timeSinceCreated = 0;
//Determine which alignment to use. The parent class uses string constants to pass this
//information quickly and easily.
switch (alignment) {
case "center": thisTextFormat.align = TextFormatAlign.CENTER; break;
case "left": thisTextFormat.align = TextFormatAlign.LEFT; break;
case "right": thisTextFormat.align = TextFormatAlign.RIGHT; break;
case "justify": thisTextFormat.align = TextFormatAlign.JUSTIFY; break;
}
//Set the permanent display option if the message needs to stay on the screen.
this.permaDisplay = permaDisplay;
//Set the font. The parent class uses string constants to pass in the embedded font
//quickly and easily.
thisTextFormat.font = font;
//Set the size.
thisTextFormat.size = size;
//Set the format properties to the text.
thisTextField.defaultTextFormat = thisTextFormat;
//Add the text to the text box and end the line with a newline character.
thisTextField.appendText(myMessage + "\n");
}
public function clearMessages() {
//Clear the text box.
thisTextField.text = "";
this.permaDisplay = false;
}
public function updateMessages() {
//This function is called every frame.
//Decrement the displayTimer variable.
displayTimer--;
timeSinceCreated++;
//Set the limits for each timer variable.
if (displayTimer < 0) {displayTimer = 0;}
if (timeSinceCreated > 60) {timeSinceCreated = 60;}
//If permaDisplay is true, give 1 second of life.
if (permaDisplay == true) {displayTimer = 60;}
//Determine the visibility of the text box. If the text box has less than 1 second
//to live, gradully fade it out.
if (timeSinceCreated < 60) {thisTextField.alpha = timeSinceCreated / 60;}
else {thisTextField.alpha = displayTimer / 60;}
}
}
}