Creating a Search Application

In this example, we'll create a project that will search for a phone number from within an array based on the person's name.


Open phoneNumberSearch1.fla in the Lesson04/Start folder.

This file contains two layers: Actions and Search Assets. The Actions layer will contain the script for this project. The Search Assets layer contains the text fields, button, and graphics for this exercise.

In this exercise, you'll produce a simple application that lets you enter a name in a search field to return the phone number for that individual. Two text fields are on the screen: nameField will be used to enter the name to search; resultField will display the search result. An invisible MovieClip over the Search button graphic has an instance name of searchButton that will be used to call a search function.


With the Actions panel open, select the first frame in the Actions layer and add the following script;

    var directory:Array = [{name:"John", phone:"919-555-5698"},¬       {name:"Kelly", phone:"232-555-3333"},¬       {name:"Ross", phone:"434-555-5655"}];

This script creates an array called directory containing three objects, each of which has two properties: name and phone.

To access the first name in this array, you use directory[0].name; the first phone number would be accessed using directory[0].phone. This syntax represents John's name and number. The syntax will play an important role in the search function we're about to script.


Add this function definition after the directory array:

   function getPhoneByName(name:String):String {      for(var i:Number = 0; i < directory.length; i++) {        if(directory[i].name.toLowerCase() == name.toLowerCase()) {          return directory[i].phone;        }      }      return "No Match";    }

You just defined the function that will search the directory for a specific phone number. This function takes a name as a parameter and returns the result as a string. When this function is called, it will iterate over the directory until it finds a name that matches the name passed into the function.

The first thing we need to do in this function is create a for loop that loops as many times as there are items in the directory array.

Inside each loop we will create an if statement that compares the name property of the current object in the loop with the name passed into the function. We use the toLowerCase method of the String class on both strings in the statement to make the search case-insensitive.

If the condition evaluates to TRue, we return the value of the phone property of the current object in the loop.

If no match is found after the loop completes, we will return the string No Match.


Add this script after the getPhoneByName() function:

   searchButton.onRelease = function() {      resultField.text = getPhoneByName(nameField.text);    }

The script adds an onRelease event handler to the searchButton MovieClip. When the Search button is released, the resultField text property is set to the result of the getPhoneByName() function. We're passing in the value of the nameField as the parameter of our search function.


Chose Control > Test Movie. Enter John, Kelly, or Ross in the search field and then click the Search button. Enter any other name and click the Search button.

A phone number should appear when a name is valid, and "No Match" should appear when no match is found in the directory array.


Close the test movie and save your work as phoneNumberSearch2.fla.

In this exercise, you used a simple loop to create a search routine.

Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221 © 2008-2017.
If you may any questions please contact us: