WORKING WITH STRING AND SELECTION OBJECTS


As one of the most commonly used objects, the String object uses methods that can be very helpful for modifying and building strings quote-enclosed values that contain information (like the name "Jobe") that can be easily understood by humans.

 message = "No shoes, no service!" 

The above ActionScript creates a variable named message whose value is a string. In such cases, you can think of the variable itself as an instance of the String object which means you can use String object methods to manipulate its value.

Let's look at a few examples.

The toUpperCase() method of the String object forces a string to become all uppercase letters. For instance:

 message = message.toUpperCase(); 

The above script modifies the variable message to contain "NO SHOES, NO SERVICE!" (For the opposite effect that is, to force the entire string to become all lowercase letters you would apply toLowerCase() in the same fashion.)

Note that the text value of a text field instance (the text within the field) is considered an instance of the String object. Thus, if message were the name of a text field instance, the previous script could be rewritten as follows:

 message.text = message.text.toUpperCase(); 

Is there any advantage of using one over the other? Not really: Every project is different, and you may find cases where one seems to be more appropriate than the other. To review, you can create an instance of the String object by:

  • Using a constructor (for example, myNewStringObject = new String("Hello"); ). The String object is identified as myNewStringObject

  • Assigning a string value to a variable. The String object is identified as the name of the variable

  • Creating a text field. The String object is identified as nameOfTextField.text

Since most of the projects in this book contain text fields, you'll find that the most common reference we make is nameOfTextField.text .

Another useful method of the String object indexOf() lets you find the first occurrence of a character or group of characters in a string. The result returned is a number corresponding to the letter index where the string starts. A letter index is the number of a character in relation to the whole string. The first character in a string has an index of 0; the second has an index of 1; and so on. If the indexOf() method finds no occurrences of the character or group of characters, it returns a value of 1. The following is an example of one use of the indexOf() method:

 message.text = "No shoes no service!";  firstS = message.text.indexOf("s"); 

The variable firstS will be assigned a value of 3 because that's the character number of the first s encountered.

graphics/04fig10.gif

It can sometimes be useful to determine the number of characters in a string. This is easy to do because all instances of the string object have a length property. You can often use string length to validate user-input information in a text field. Say, for example, that you wanted a user to enter a valid U.S. Zip code; you would know that it must be five characters in length. By checking the length property of the Zip code, you could create a simple validation script like the following:

 zipCode.text = "27609";  zipLength = zipCode.text.length  if (zipLength == 5) {    // Correct length of zip code  } else {    // Invalid zip code  } 

The first line sets the text value shown in the zipCode text field. (We assume this is what the user has entered.) The next line creates a variable named zipLength and assigns it a value based on the length property of zipCode.text in this case 5, because that's the number of characters the zipCode field contains. The last part of the script uses an if statement to take one set of actions if zipLength equals 5 and another if it doesn't.

The Selection object allows you to control various aspects of the currently focused text field, including highlighting text, getting and setting the caret's (current insertion point) position and more. A text field is considered focused if the user's cursor is placed there. Since only one text field can be focused at a time, there's never more than one Selection object.

By clicking on a text field, the user dictates which one has focus. However, you can use the setFocus() method to override the user's current choice important since you can only use other Selection object methods on the text field currently in focus. (As you'll see from the following exercise, you can't always rely on the user to select and thus bring into focus the proper text field.)

One last method of the Selection object allows you to highlight portions of text dynamically without the user's help. This method (Selection.setSelection(param1, param2) ) includes two parameters: the character index of where the selection starts and the character index of where the selection should end. Say, for example, you have a text field that contains the text "Derek is the craziest person I know." To highlight the word craziest (assuming its text field is in focus), you would use the following:

 Selection.setSelection(13, 20); 

graphics/04fig11.gif

In this exercise, you'll create a simple word processor using many of the String and Selection object methods we've just discussed.

  1. Open wordProcessor1.fla in the Lesson04/Assets folder.

    There are two layers in this file, Background and Buttons. The Background layer contains the image of the word processor window as well as text fields. The Buttons layer contains the four buttons that appear at the top of the word processor window. There are three text fields on the stage. The largest one, in the center, represents the text document. It has an instance name of inputField. The next text field, to the right of the Find button, is appropriately called findField. When played, the user will be able to enter a character or string of characters into this text field to search the inputField text field. The search results will be displayed in the third text field, at the bottom of the window, which is called status . This text field is also used to display the results when counting the number of characters in the document. We will be attaching various scripts to the buttons at the top of the window to make our word processor work.

    graphics/04fig12.gif

  2. With the Actions panel open, select the Uppercase button (with the capital A) and add the following script:

     on (release) {    inputField.text = inputField.text.toUpperCase();  } 

    Flash interprets this script as saying, "On release of this button, make the letters in the inputField text field uppercase." When the button is pressed, the contents of the inputField text field are reassigned with uppercase letters.

  3. With the Actions panel open, select the Lowercase button (with the small a) and add the following script:

     on (release) {    inputField.text = inputField.text.toLowerCase();  } 

    This script is similar to the one added in the previous step. On release of the button, the inputField text field is reassigned with all lowercase characters.

  4. With the Actions panel open, select the Character Count button (with '123') and add the following script:

     on (release) {    status.text = "There are " + inputField.text.length + " characters in the  current document";  } 

    When this script is executed, a custom message will be displayed in the status text field. The Length property of the inputField text field is determined and inserted in the middle of the message. The message is built dynamically by adding, "There are " plus the value of the length property, plus the ending part of the message, " characters in the current document." If the document has 50 characters, the message will read, "There are 50 characters in the current document."

  5. With the Actions panel open, select the Find button (with the magnifying glass) and add the following script:

     on (release) {    result = inputField.text.indexOf(findField.text);    if (findField.text != "" && result >= {      status.text = "The first instance of these characters occurs at character  " + result;      Selection.setFocus("_root.inputField");      Selection.setSelection(result, result + findField.text.length);    } else {      status.text = "That string could not be found";    }  } 

    The above script is the most complex one in this exercise because it does several things. The first line sets the value of the result variable using the indexOf() method of the String object. Here, we're asking the script to look in the inputField text field and find the index number of the first occurrence of the character or group of characters entered into the findField text field. In the end, result will have a value of 1 (no occurrence was found) or 0 to the string length (depending on how many characters are in the document). For example, if the first occurrence is found at index 13, result will have a value of 13. This value plays an important part in the rest of the script.

    graphics/04fig13.gif

    The next part of the script uses an if statement to carry out one of two sets of actions, depending on whether a couple of conditions prove true. The first part of the statement looks at the text values of findField and the numeric value of result . It says that if findField is not empty (this makes sure that the findField text field has not been left blank) and the value of result is equal to or greater than 0 (which it will be if an occurrence of the characters entered is found), do the actions below. The else part of the statement deals with what happens if neither of these conditions is met. In this case, nothing will happen except the status text field will display the text "That string could not be found."

    Assuming that both conditions are true, let's look at what the three actions under the first part of the if statement do. The first action will display a dynamic text message in the status text field. The message is built dynamically by adding "The first instance of these characters occurs at character " plus the value of result . If result has a value of 7, the message will read, "The first instance of these characters occurs at character 7." The next two actions use methods of the Selection object to highlight the character or characters in the inputField text field that were searched for and found. You'll remember that a text field must have focus before any of the Selection object methods can be used on it. Since we want to highlight text in the inputField text field, we use the following line to ensure that this field has focus before the next Selection method is used on it:

     Selection.setFocus("_root.inputField"); 

    The next line of script uses another Selection method to highlight text in the currently focused text field. This line of script reads as follows:

     Selection.setSelection(result, result + findField.text.length); 

    We've used a couple of dynamic values with this method to determine where the selection begins and ends. Because we want the selection to begin where the first occurrence was found, we use the value of result as the starting point of the selection. The ending point of the selection is determined by adding the value of result to the value of the length property of the findField text field. How does this work? Assume you typed "I like my dog very much" in the inputField text field. Next, in the findField text field, you search for "dog." When the above script is executed, result will have a value of 10 (the first occurrence of "dog" is at the tenth character) and findField.text.length will be 3 (since the word "dog" is currently in this field, and it is three characters long). Using these values, the above line of script could be written as follows:

     Selection.setSelection(10, 10 + 3); 

    or

     Selection.setSelection(10, 13); 

    This will highlight Characters 10 through 13, which is where "dog" appears in the inputField text field.

  6. Choose Control > Test Movie.

    Type into the inputField text field and press the Uppercase and Lowercase buttons. Also press the Count Characters button to determine how many characters the document contains. Finally, try out the search function by entering one or more characters into the findField text field and pressing the button.

  7. Close the test movie and save your work as wordProcessor2.fla.

    You can see how easy it is to use the methods associated with the String and Selection objects a good thing since you'll find yourself using the String object frequently to both validate and reformat data.



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net