1. Code
  2. ActionScript

Display Suggestions in a TextField using AS3 and External Files


Suggested Terms is an excellent usability element which helps the user select a better option or just increase search speed.

In this tutorial, we will learn how to create and display suggested terms in a Flash application.

Step 1: Overview

We'll make use of TextField and String methods and properties to retrieve and display words from an external file containing the search suggestions.

Step 2: Document Settings

Launch Flash and create a new document. Set the stage size to 500x270px, background color to #F6F6F6 and the frame rate to 24fps.

Step 3: Interface

This is the interface we'll use, a simple background with a title bar and a two TextFields, an Static TextField telling us what to do and an Input TextField that we'll use to start suggesting.

No buttons this time, the events will be called by pressing a key.

Step 4: Background

You can leave the background color as it is or add 500x270px rectangle to have something you can select. For the title bar, use again the Rectangle Tool (R) to create a 500x30px rectangle and center it.

Step 5: Title

Select the Text Tool (T) and write a title for your application. I used this format: Lucida Grande Regular, 15pt, #EEEEEE.

Step 6: Text Area

We'll use a Rectangle shape to show where the TextField is.

With the Rectangle Tool, create a 300x24px rectangle and remove the fill, instead, use a #CCCCCC stroke.

Step 7: Input TextField

Lastly, use the Text Tool to create a 345x20px Input TextField and name it inputField.This is the format I used: Helvetica Bold, 16pt, #666666.

Step 8: Font Embedding

To display the font correctly in the Input Text we'll have to embed it.

Select the Input TextField and go to the Properties panel, Character section and press the Embed... button.

A new window will come up, select the characters you want to embed, and click OK.

Step 9: New ActionScript Class

Create a new (Cmd + N) ActionScript 3.0 Class and save it as in your class folder.

Step 10: Package

The package keyword allows you to organize your code into groups that can be imported by other scripts, its recommended to name them starting with a lowercase letter and use intercaps for subsequent words for example: myClasses. It's also common to name them using your company's website: com.mycompany.classesType.myClass.

In this example, we're using a single class, so there isn't really a need to create a classes folder.

Step 11: Import Directive

These are the classes we'll need to import for our class to work, the import directive makes externally defined classes and packages available to your code.

Step 12: Declare and Extend the Class

Here we declare the class using the class definition keyword followed by the name that we want for the class, remember that you have to save the file using this name.

The extends keyword defines a class that is a subclass of another class. The subclass inherits all the methods, properties and functions, that way we can use them in our class.

Step 13: Variables

These are the variables we'll use, read the comments in the code to find out more about them.

Step 14: Constructor

The constructor is a function that runs when an object is created from a class, this code is the first to execute when you make an instance of an object or runs using the Document Class.

Step 15: External File Contents

The terms to suggest will be stored in an external text file, you can also use XML, PHP or the format of your choice.

Write the terms you want to suggest (separated by commas "," ) and save the file in the same directory as your swf, in this case I used a list of sports and saved them in the file Sports.txt.

Step 16: Load External File

This line calls the load method of the URLLoader class and passes as parameter the url of the txt file we are using.

Step 17: Initial Listeners

Two initial listeners; one listens for the load of the external file and other listens for key up events in the Input TextField.

Step 18: Suggestions Text Format

Sets the text format used in the suggestions textfields.

Step 19: Loaded Data

The following function is executed when the external load is complete, it creates an array containing the comma-separated strings in the txt file.

Step 20: Suggest Function

The suggest function handles all the operations to create and display the suggestions, is executed when the Input TextField detects a Mouse_UP event.

Step 21: Reset

The first thing to do is clear the suggested array, this will erase the previous suggestions (if any).

Step 22: Search Available Data

The next for loops through the available suggestions and uses an if statement and the indexOf method to search for the starting letters of any of the available words.

Since all the words in the example text file are in lower case, we can call toLowerCase() on the input text to allow for case-insensitive searching. This means that if the user types "SKI" it will find "skiing".

Step 23: Create Suggestions TextFields

If the written letter(s) are found, a new TextField is created for the corresponding word, since we are still in the for, if more than one suggestion starts with the same letter(s), then many TextFields will be created.

Step 24: Clear TextFields

If the user deletes the letters in the Input Field, the suggestions are removed.

Step 25: Keyboard Control

The next code allows the user to move through the suggestions using the keyboard.

It changes the color of the selected word, adds or removes a number to the currentSelection variable to use it later in the textfields array, this way it retrieves the correct item from the suggestions.

When the enter key is pressed, the selection is written in the Input Field and the suggestions are removed.

Step 26: Mouse Control

This function is also used to select the suggestion, although this is easier because of the ability to add event listeners to the TextFields. The listeners were added in the suggest()function in Step 23, remember?

Step 27: Document Class

Go back to the FLA and in the Properties Panel > Publish section > Class field, add Main as value. This will link this class as the Document Class.


You're done creating and implementing a suggested terms class, it's time to make your own and customize it! Why not try using PHP to save the terms that users enter to the list of suggested terms?

Thanks for reading this tutorial, I hope you've found it useful!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.