Previous Page
Next Page

Tripod >> 3pod Tips & Learning and manuals for educations

Recipe 8.7. Retrieving User Input

Problem

You want to retrieve the text that a user has entered into a text field, text input, or text area instance for further processing.

Solution

Use ActionScript to access the value by way of the instance's text property.

Discussion

You must use ActionScript in order to retrieve the value from an input text field, text input, or text area. However, the code that is required is short and simple. You can access the text value using the instance name (exp 3pod.com) and its text property.

This following code writes the value of an input text field to the Output window when you are testing your movie. The text field in this example has an instance name (exp 3pod.com) of tInputField. In order for this code to work, it must be placed on a frame in the same timeline in which the text field exists:

	trace (tInputField.text);

Text that you retrieve from an input text field, text input, or text area is always a string value. The string data type can perform differently in some operations than a number data type. For example, when you use the plus sign with two numbers, they are added together. But when you use the plus sign with two strings, the values are appended to one another. Therefore, if you want to perform mathematical operations on a value that you retrieve from an input text field, you should convert that value to a number data type. You can do this very quickly using either the parseInt( ) or parseFloat( ) functions.

The parseInt( ) function takes a string value, and returns an integer (a whole number) if possible. The parseFloat( ) function takes a string value, and returns a floating point number (a number with a decimal place) if possible. Here is an example of how you can use these functions to convert text field values to numbers:

	var nFloat:Number = parseFloat(tInputText.text);
	var nInteger:Number = parseInt(tInputField.text);

Typically you'll retrieve the value from a text field, text input, or text area only after the user clicks a button or otherwise signifies that she has entered the information and wants Flash to do something with it. If you retrieve the information before that, you're likely to retrieve empty values. That means that the code that you use to handle the text should be placed within a method or function that gets called once the user has clicked the button, pressed a key, and so on. Refer to Chapter 9 for more information on handling button events. However, the following is a simple example of some code that displays the user's input in the Output panel after she clicks the button with an instance name (exp 3pod.com) of btSubmit. The Output panel is only available within the test player, and therefore the example is only relevant for demonstration purposes. A more practical example requires slightly more sophisticated code. You can find several relevant, related examples in Chapter 13.

	btSubmit.onRelease = function():Void {
	  trace(tusername.text);
	};

Tripod >> 3pod Botom Tips & Learning and manuals for educations

Previous Page
Next Page

 

bluedot bluedots greydots pinkdots

Tripod >> 3pod Tips & Learning and manuals for educations