If you're used to using a word processor, it probably seems pretty natural to format your text with menus and the Properties panel. After all, they aren't that much different from Microsoft Word menus and dialog boxes. On the other hand, if you want to make major changes to text on the fly, or if you want to format text with HTML (hypertext markup language) or CSS (Cascading Style Sheets), you have to use ActionScript to produce your text. You saw some text and ActionScript examples in Chapter 12 (Strings). You'll find more here. First, the elementary basics, which begin with string theory.
Flash has lots of text containers, like TLF editable and Classic dynamic text, and then there are the components: Label, TextArea, and TextInput. All of these textdisplaying tools use the String data type (Strings). As far as ActionScript is concerned, a string is similar to an array, in that it's a list of characters. The list can be a single character or hundreds of characters. Each character is in a specific position in the list—its index number (Boolean). Arrays and strings both begin counting at position zero (0). So if the string is "Stutz Modern Motorcars" that capital S is at position 0.
Strings are common to most programming languages, and there are loads of books that explain how to perform string manipulation magic. Most Flash programs don't require such trickery. This chapter explains some of the most common techniques used by Flash designers. However, if you're an aspiring string magician, you may want to start out with or ActionScript 3.0 Cookbook or Essential ActionScript 3.0, both published by O'Reilly.
You want to use the name of your company, "Stutz Modern Motorcars," in an ActionScript program. The typical way to do so is to create a variable with the data type string, and then store the name of your company inside that variable. Whenever you need the company name to appear in your program, you can provide the name of the variable. Here's the way you'd create a string variable for your company name:
var strCompanyName:String = "Stutz Modern Motorcars";
This statement does a few things in a single line. It creates and names a string variable strCompanyName, and then it assigns a string value to the variable. You only have to create the variable with the var statement once, and then you can use it as many times as you want. In this case, the variable name begins with the three letters "str" to indicate that it's a string, but that's not necessary. The variable name could be a single letter.
String values, like "Stutz Modern Motorcars" are always shown within either double or single quotes (Strings). One of the reasons you can use either type of quotes is that it provides an easy way to include quotes within your string. So here are examples of valid strings:
strDont = "don't"; strQuote = 'Ed said, "I love my Stutz Bearcat. I drive it everywhere."';
The important rule is that you have to begin and end the string with the same type of quotation mark.
Because you assign your strings to variables, you can change the strings' values. For example, if the boss changes the name of Stutz Modern Motorcars to the simpler (and even more modern) Stutz Motor Company, you can update your ActionScript code by assigning the new name to the strCompanyName variable, like so:
strCompanyName = "Stutz Motor Company";
One of the most common ways to modify a string is to add more text to it. In geekspeak that's called concatenation, but you can think of it as joining strings. Suppose your boss finally gets around to filling out those incorporation papers, and your company has yet another new name. If you want to add the word "Incorporated" to the existing strCompanyName, here's how you'd do it:
strCompanyName = strCompanyName + ", Incorporated";
If you want to test some of these string examples, you can use the trace() statement to display the string in the Output panel. For example, to display strCompanyName, add this line to your ActionScript code:
trace(strCompanyName);
Make sure all the string variables are declared with a var statement like this:
var strCompanyName:String = "Stutz Modern Motorcars";
If they're not declared before they're used, you'll get an error.
As you can see, you're adding a new string value to the end of the existing string. The end result is a complete name "Stutz Motor Company, Incorporated." Joining strings is such a common and popular task that there's a shortcut to help you do so with few keystrokes. It looks like this:
strCompanyName += ", Incorporated";
That line of code does exactly the same thing as the preceding example, just with fewer keystrokes. Make sure you keep the + sign to the left of the = sign. When you do it the other way around, the code has an entirely different meaning to ActionScript.
When you have a string inside quotes, like the ", Incorporated", it's known as a string literal. The string is literally what's inside the quotes, similar to a constant. When a string is represented by a variable, like strCompanyName, it's a string variable. In most situations, you can use either representation. For example, here's another way you can construct the new company name:
var strCompanyName:String = "Stutz Motor Company"; var strInc:String = ", Incorporated"; strCompanyName += strInc;
You can use a combination of string literals and string variables when you're joining a string. You see this technique in use when websites greet you by name. An ActionScript example looks like this:
var strVisitorName = "Chris Grover"; strGreeting = "Hello, " + strVisitorName + ". What can I do to put you in a Stutz Bearcat today?";
When you join strings with the concatenation operator (+ or +=), everything inside the quotes has to be on a single line. That's one of the reasons you see multiple assignment statements when an ActionScripter is creating a long paragraph of text.
var strSalesPitch:String = "The legendary Stutz Bearcat."; strSalesPitch += "It's your best value in high-performance "; strSalesPitch += "sport cars today."; strSalesPitch += "At the Stutz Motor Company, we want to know,"; strSalesPitch += "What can we do to put you in a Stutz Bearcat today?";
This block of text produces a single long string. Even though there were line breaks in the ActionScript code, those have no effect on the string when it is displayed. The line breaks are dictated by the properties and size of the text container. To see how you specifically place line breaks in a string, see the next section.
This last example is shown in 17-1_Join_Strings.fla in the Missing CD (www.missingmanuals.com/cds). A trace statement added to the code displays the completed string in the Output panel.
When you're using objects of the TextField class, you can use the appendText() method. In fact, appendText() is preferred because it runs faster than the concatenation operator. Here's some code that shows appendText() in action:
1 var tfBearcatBanner:TextField = new TextField(); 2 var strSalesPitch:String = new String(); 3 strSalesPitch = "Stutz Motor Company\nHome of the legendary Stutz Bearcat\n"; 4 tfBearcatBanner.text = strSalesPitch; 5 tfBearcatBanner.appendText("What can we do to put you in a Bearcat today?"); 6 7 tfBearcatBanner.x = 40; 8 tfBearcatBanner.y = 40; 9 tfBearcatBanner.width = 280; 10 tfBearcatBanner.height = 160; 11 12 addChild(tfBearcatBanner);
The first two lines create a text field and a string. The third line puts some text in the string strSalesPitch. Line 4 assigns that string to the text property of the text field. Line 5 is where the appendText() method comes in. The text in the parentheses is a string literal, but it could just as easily be a variable. Lines 7 through 10 position and size the text field on the stage. The last line adds the text field to the Display List, which makes it visible on the stage. When you run this bit of code, Flash Player displays the text as shown in Figure 17-2. This example is called 17-2_Append_Text.fla in the Missing CD (www.missingmanuals.com/cds).
When you're typing text into a text field, you hit the Enter (or Return) key to begin a new line, but that doesn't work when you're creating a string in ActionScript. You need to insert a special signal within the string. It's a backslash with a lowercase n, which stands for new line. Here's an example:
strSalesPitch = "Stutz Motor Cars \n Home of the legendary Stutz Bearcat";
If placed in a multiline text field and given enough room to display, this string appears as two lines:
Stutz Motor Cars
Home of the legendary Stutz Bearcat
There's a space after the word "Cars" and before the word "Home," because, after all, strings are literal. To avoid the extra space at the beginning of the second line, you have to eliminate the space characters on both sides of \n. It looks strange to us humans, because we see individual words, but that's not the way ActionScript sees a string. ActionScript just sees a long line of characters: letters, numbers, spaces, and punctuation. The only thing that really grabs its attention is the sequence \n. When ActionScript sees that backslash followed by a lowercase n, it knows that's the signal for a new line.
Suppose your Flash animation is more like a computer program. It's so complex that you've provided 200 pages of help text to help your audience learn how to use it. You want to create some sort of search function so folks can zero in on the help they need. A string that's inside another string is often called a substring, and ActionScript provides some methods to help you find a substring inside a larger string.
To make it simple, this example is going to search for the word "legendary" within the longer string "Home of the legendary Stutz Bearcat." Not quite hundreds of pages, but you get the idea. All strings inherit the same properties and methods from the string class. Two of those methods are indexOf() and lastIndexOf(), and they're specifically used to search for substrings. As with any good method, you put them to work by tacking them onto the end of an object. Here's some code to show how it works:
var strSalesPitch:String = "Home of the legendary Stutz Bearcat." strSalesPitch.indexOf("legendary");
The first line creates the string variable named strSalesPitch. The second line runs the string method indexOf(). The method needs to know what substring you're searching for, so you provide that as a parameter. You can put the string literal right inside the parentheses, as shown here with "legendary," or you can provide a string variable, which, of course, wouldn't include the quotes.
So, what does the indexOf() method do? It gives you back a number. Specifically, it gives you the index number where the searched-for string begins. In the case above, strSalesPitch.indexOf("legendary") is equal to 12, because if you start counting at zero, and you count the letters and spaces, you find that the letter "l" is at index 12. The lastIndexOf() method works in a similar manner, except that it starts searching from the end of the string rather than the beginning. If either method is unable to find the substring, it gives back the number –1. That result is actually helpful, since you can use it with conditional statements. For example, this code would work:
if (strSalesPitch.indexOf("legendary ") == -1) strComment = "Not legendary.";
The if() statement tests to see if the string "legendary" is part of strSalesPitch. If the statement doesn't find that string, the indexOf() method returns –1. When that condition exists, the words "Not legendary" are assigned to strComment. It's also useful to partner the "does not equal" (!=) operator with indexOf() and lastIndexOf(). So you can write code that says something like the following:
if (strSalesPitch.indexOf("legendary") != -1) strComment = "This is a legendary automobile.";;
In other words, if the code finds the substring, it assigns the string "This is a legendary automobile." to strComment.
Search and replace go hand in hand in the computer world. Suppose there were changes at the car dealership and you needed to make changes to your sales pitch: "Home of the legendary Stutz Bearcat." You can use the replace method. It works like this:
strSalesPitch = strSalesPitch.replace("Stutz Bearcat","Toyota Prius");
As usual with methods, you tack replace() onto the end of the object, in this case, a string. For parameters, you provide the search words or letters, and then you provide the replace words or letters. Also, as usual, you use a comma to separate parameters when there's more than one. The replace() method works with both string literals, like the ones shown here, or string variables.
You can change the case of a string using the toUpperCase() or toLowerCase() methods. Part of the String class, you use these the same way as the other methods. Continuing with the car dealership theme, here are some examples that use trace() to display the strings in the Output panel:
var strSalesPitch = "Home of the legendary Stutz Bearcat"; trace("This is the initial string: \n" + strSalesPitch); trace("\nThis is toUpperCase: \n" + strSalesPitch.toUpperCase()); trace("\nThis is toLowerCase: \n" + strSalesPitch.toLowerCase());
The first line creates the strSalesPitch variable and assigns the words "Home of the legendary Stutz Bearcat" to the variable. The next line uses trace() to send the string to the Output panel. (As mentioned on Using Event Properties, the trace() statement is a favorite debugging tool of ActionScripters.) The first string inside the trace() statement's parentheses is a string literal that explains what's to follow: "This is the initial string:". The \n is the new-line character that forces the following text to start on a new line. It works in the Output panel the same way it works in a text field. The + (concatenation operator) joins the two strings. The last string is the variable strSalesPitch. The final two lines are nearly identical to the second line. They add one more \n to provide some additional, helpful white space. Finally, the methods are applied to the strings. What appears in the Output panel comes as little surprise:
This is the initial string:
Home of the legendary Stutz Bearcat
This is toUpperCase:
HOME OF THE LEGENDARY STUTZ BEARCAT
This is toLowerCase:
home of the legendary stutz bearcat
Enough with the string theory. The next sections show how to create text on the fly using ActionScript and how to format that text using a few different tools. This example is named 17-4_Change_Case.fla in the Missing CD (www.missingmanuals.com/cds).