Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

Manipulate Text Field with Flash ActionScript 3

Like other programming languages, Text Fields are not limited to handle strings (texts). Text Fields also used to handle numbers. In this Flash ActionScript 3 tutorial, we use Text Fields to handle numeric values and do some arithmatic calculation.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

Text Fields are not limited to handle strings (texts). We will sometimes use Text Fields to handle numbers and do arithmatic calculation.

However this is important to ensure that only numbers are allowed to enter into the Text Fields and the Text Fields are not empty. Luckily, this is rather easy to accomplish this with ActionScript 3.

Input numbers in the two text Fields in the above Flash Movie and click the Enter button to see how it works.

Flash ActionScript Codes:

// We need Keybpard control
import flash.events.KeyboardEvent;

 

// Allow Text Fields to input data
inputText1_txt.type = TextFieldType.INPUT;
inputText2_txt.type = TextFieldType.INPUT;

 

// Set the mouse cursor to focus on the first field
inputText1_txt.stage.focus = inputText1_txt;

 

// Only allow number to input into the two fields
inputText1_txt.restrict = "0-9";
inputText2_txt.restrict = "0-9";

 

// Function to do the calculation
function readText(evt:Event):void {

// Check if data are entered in the two fields
if (inputText1_txt.text == "" || inputText2_txt.text == ""){
output_txt.text = "Fields cannot be Empty!";
} else {
var firstNumber = Number(inputText1_txt.text);
var secondNumber = Number(inputText2_txt.text);

total_txt.text = firstNumber + secondNumber;
}

// Hook up the button with the function readText
enter_btn.addEventListener(MouseEvent.CLICK, readText);

 

// Function that ensure only numbers are allowed to input
function numberCheck(event:KeyboardEvent):void {

if (event.charCode >= 48 && event.charCode <= 57) {
output_txt.text = "";
} else {
output_txt.text = "Only numbers are allowed.";
}
}

// Hook up the two Text Fields with the function numberCheck
inputText1_txt.addEventListener(KeyboardEvent.KEY_DOWN, numberCheck);
inputText2_txt.addEventListener(KeyboardEvent.KEY_DOWN, numberCheck);

Download Flash Source File:

Flash Source File text-2.fla

Remarks:

Now we know how to use Dynamic Text Fields to handle strings and numbers with Flash ActionScript 3.