Exploring HTML Usage in Text Fields


As you have been using Flash 8, you may have noticed the HTML button in the Property inspector for text fields. Even though one of the primary advantages of using Flash movies is that you can avoid the fuss of HTML page layout, you can use HTML formatting tags within Input and Dynamic text fields. You can use <font> tags to specify multiple typefaces, colors, styles, and sizes within one text field. You can also use <a href> tags to link to internal ActionScript functions or external URLs!

Caution 

In Flash Player 4 movies, you cannot specify more than one set of formatting specifications for any text field. For example, if you create a text field that uses black Verdana text at 18 points in faux bold, you can't insert any other typeface, color, or size in that text field.

Supported HTML Tags

You can use the following HTML tags to format your Flash text fields. You can insert these tags into ActionScript variable values, or you can apply them (without knowing or writing the syntax) using the Property inspector. As you already know, text fields in ActionScript are real objects with instance names. As such, you need to address specific properties of the TextField object to insert HTML text. In previous chapters, you've used the following syntax to assign text to a text field instance:

 var tLastName:TextField; tLastName.text = "Enter your last name here"; 

You cannot assign HTML tags in the text property of a TextField object. You must use the htmlText property to assign HTML formatted text, such as:

 var tLastName:TextField; tLastName.html = true; tLastName.htmlText = "Enter your <b>last</b> name here."; 

Here, the <b> tag is used to bold the text "last" in the text field instance. In a moment, you will create your own examples that use the htmlText property. You also need to set the html property of the TextField instance to true, to let the Flash Player know that it should parse the HTML text you specify for the htmlText property. Let's review the HTML tags that are available in ActionScript.

Font and Paragraph Styles

The basic <font> and physical "faux" styles for text (bold, italic, and underline) can be applied to Flash text.

  • <b>: Placing <b></b> tags around Flash text in string values for text field variables applies bold formatting to the enclosed text.

  • <i>: Placing <i></i> tags around Flash string values italicizes the enclosed text.

  • <u>: The <u></u> tags underline the enclosed text.

  • <p>: The <p> tag inserts a paragraph break between lines of text. You can use the align attribute to specify left, right, center, or justify, applying the respective justifications to the Flash text.

    New Feature 

    The justify value for the align attribute is now supported in Flash Player 8 for HTML text.

  • <br/>: The <br/> tag inserts a carriage return at the point of insertion. This is equivalent to the newline operator in ActionScript.

  • <font color='#hex code'>: The <font> tag with the color attribute can change the color of your Flash text. This color is specified in hexadecimal values, just as with regular HTML. For example, "<font color='#FF0000>This is red text.</font>" uses full red for the text color.

  • <font face>: The <font> tag with the face attribute enables you to specify a specific typeface to the enclosed text. You can specify Flash device fonts for the face value, such as <font face='_sans'> to use the Sans Serif device font.

  • <font size>: The size attribute of the <font> tag enables you to specify the point size of Flash text. You can use absolute values (in point sizes), such as <font size='18'>, or relative values, such as <font size='+1'>, to change the size of text.

  • <font letterSpacing>: The letterSpacing attribute of the <font> tag enables you to uniformly add horizontal space around each character. You can use positive or negative integers, such as <font letterSpacing='18'>. This attribute is only supported in Flash Player 8.

  • <font kerning>: The kerning attribute of the <font> tag enables you to turn kerning on (1) or off (0). Kerning is the space between characters of a specific font, and is not available for some fonts such as Courier New.

  • <textformat>: This tag is a Flash-specific formatting tag that you won't find in traditional HTML. <textformat> has four attributes, indent, leading, leftmargin, and rightmargin, that control the margin and line spacing of text within the text field. Each of these attributes uses pixels as the unit of measurement. To get a feel for how these attributes work, create a Static text with a paragraph of text, click the Format button in the Property inspector for the selected text field, and change the settings in the Format Options dialog box.

    Tip 

    You can only use the <textformat> tag within text fields for Flash Player 6 or higher movies. Make sure the Version menu in the Flash tab of the Publish Settings (File ð Publish Settings) is set to Flash Player 6 or higher. Flash Player 8 supports negative values for text indent as well.

  • <span>: As with its real HTML counterpart, <span> enables you to apply a style from a style sheet to the encompassed range of text. This tag has only one supported attribute, class. This attribute can be set to the name of the style class declared in the style sheet. For example, <span > applies the style named heading to the enclosed text.

  • <img>: A potentially exciting tag supported in Flash HTML is the <img> tag, short for image. As you may have guessed, this tag enables you to insert images inline with other text in a text field. This tag supports the following attributes: src, id, width, height, align, hspace, and vspace. The src attribute can be set to one of the following: the linkage identifier of a symbol in the library, the external URL to an .swf file, or the external URL to an image file. The JPEG, progressive JPEG, GIF, and PNG formats are supported in Flash Player 8. Flash Player 7 only supports standard JPEG files with the <img> tag.

URL and ActionScript Linking

You can use the anchor, or <a>, tag with the href attribute to apply URL links within Flash text fields. For example, you can insert the following HTML into a string value for the htmlText property of a text field instance, to link the text [theMAKERS] Web site to the appropriate URL:

 <a href='http://www.theMakers.com'>the Makers Web site</a> 

You can also specify a target attribute for the <a> tag. The target attribute determines which browser window or frame displays the URL link in the href attribute. As with regular HTML, you can use the default _top, _parent, _self, or _blank values, as described for the getURL() action. Later in this section, you also learn how to execute internal ActionScript functions from <a href> tags.

Caution 

You cannot type HTML tags directly into any text block or field — the actual tags show up in the text field as the Flash movie runs in the Flash Player. The formatting tags are specified in ActionScript code or are "hidden" in Static text. (The Property inspector applies the formatting.)

Formatting Text with the Property Inspector

You don't necessarily need to write out HTML tags to apply them to your Flash text. You can use the Property inspector to assign HTML formatting to all Text types (that is, Static, Input, and Dynamic). For Input and Dynamic text fields, you need to enable HTML formatting by pressing the Render text as HTML button in the Property inspector. In this section, we demonstrate the use of HTML formatting within Static and Dynamic text fields.

  1. Open a new Flash document (Ctrl+N or z+N). If the background color of your document is a nonwhite color, then set the background color to white in the Document Properties dialog box (Ctrl+J or z+J). Save your Flash document as htmlText_static.fla.

  2. Select the Text tool and open the Property inspector. Make sure the Text type menu (in the top-left corner of the inspector) is set to Static Text. Click once on the Stage and type the following text (with carriage returns) in the text block, using Verdana at 18 points:

     Flash 8 Bible by Robert Reinhardt & Snow Dowd 

  3. With the text block still active, select the Flash 8 Bible text and, in the Property inspector, change the point size to 24 and click the B (for bold) option, as shown in Figure 30-1. Enter the following URL in the URL field of the Property inspector and choose_blank in the Target menu:

    • http://www.flashsupport.com

  4. With the text block still active, select the Robert Reinhardt text, and, in the Property inspector, enter the following text for the URL field:

    • mailto:robert@theMakers.com

  5. Now, select the Snow Dowd text and enter the following text in the URL field of the Property inspector:

    • mailto:snow@theMakers.com

    See Figure 30-2 for an example of how the URL-linked text will appear.

  6. Save the Flash document, and test the Flash movie file (.swf) in your Web browser by choosing File ð Publish Preview ð HTML. When you click the Flash 8 Bible text, the browser loads the official support site for the Macromedia Flash 8 Bible in a new win dow. When you click either author's name, your e-mail client opens a new message window.

image from book
Figure 30-1: You can selectively change text within one text block or field.

image from book
Figure 30-2: URL-linked text appears with dashed underlines. You will not see this dashed underline in the actual Flash movie file (.swf).

On the CD-ROM 

You will find the completed htmlText_static.fla document in the ch30 folder of this book's CD-ROM.

Try making another example with your own text and URL links. You can even add javascript: commands to URL links.

Inserting HTML Tags Into Text Fields with ActionScript

In this section, we continue with the previous example that you created in the last section. You convert the Static text block into a Dynamic text field and manipulate the formatting with ActionScript.

  1. Resave your Flash document from the last section as htmlText_dynamic.fla. You will convert this Static text into a Dynamic text field, so you'll want to keep your original Static text example for future reference.

  2. Select the text block and open the Property inspector. Change the text type to Dynamic Text and make sure the HTML option is enabled. In the <Instance Name> field, type the name tBook. Now, this text field can be updated with ActionScript directed at the tBook instance. You can also disable the Show Border option if you don't want to see a bounding box around your text.

  3. Create a new layer named actions. Select frame 1 of the actions layer, and open the Actions panel (F9, or Option+F9). Add the following code to the Script pane. This code displays the htmlText value of the tBook field in the Output window when you test the movie.

     var tBook:TextField; trace("htmlText: " + tBook.htmlText); 

  4. Save the Flash document and test it (Ctrl+Enter or z+Return). While the Flash movie is playing in the Test Movie window, you should see the HTML formatting tags displayed in the Output window. The htmlText property reads as one continuous line displaying the following markup:

     htmlText: <TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE= "verdana" SIZE="24" COLOR="#000000" LETTERSPACING="0" KERNING="0"> <A HREF="http://www.flashsupport.com" TARGET="_blank"><B>Flash 8 Bible</B></A></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"> <P ALIGN="LEFT"><FONT FACE="verdana" SIZE="18" COLOR="#000000" LETTERSPACING="0" KERNING="0">by <A HREF="mailto:robert@theMakers. com" TARGET="">Robert Reinhardt</A> &amp; <A HREF="mailto: snow@theMakers.com" TARGET="">Snow Dowd</A></FONT></P></TEXTFORMAT> 

    You can observe the requisite ActionScript syntax for HTML formatting in the Output panel.

    Note 

    HTML tag names are not case-sensitive in Flash. We prefer to use XHTML-compliant code; as such, you'll see our examples use all lowercase characters in HTML tag and attribute names. The values of attributes, such as file URLs in href or src, require attention to case-sensitivity.

  5. Close the Test Movie window and go back to the Main Timeline of your Flash document. Create a new Dynamic text field, and in the Property inspector, enable the HTML option. In the <Instance Name> field, type the name tBook2.

  6. Select the first frame of the actions layer and open the Actions panel (F9, Option+F9 on Mac). In the Script pane, specify an HTML-formatted string value for the htmlText property of the tBook2 instance, such as the following code. Comment out the trace() action you inserted earlier as well. Note that the \ character is required before additional quotes within a String value. The image from book character indicates a continuation of the same line of code.

     var tBook:TextField; var tBook2:TextField; //trace("htmlText: " + tBook.htmlText); tBook2.htmlText = "<font face=\"Verdana\" size=\"24\" image from book    color=\"#0000FF\"><b><a image from book    href=\"http://www.communitymx.com\"> image from book    CommunityMX</a></b></font>"; 

    Note 

    The last line of code should appear as one line number in the Script pane of the Actions panel.

  7. Save the Flash document and test it. The tBook2 text field displays the HTML-formatted value that you specified in the actions layer.

You can also use variables in expressions for HTML-formatted text fields, such as the following (note that the bookURL variable and value should appear on one line of code):

 var sURL:String = "http://www.flashsupport.com"; var sName:String = "Flash 8 Bible"; tBook.htmlText = "<a href=\"" + sURL + "\">" + sName + "</a>"; 

By using other ActionScript variables and methods, you can apply specific text formatting to external data sources that have been loaded into the Flash movie, such as database records or lists. In the next section, you learn how to format text within text fields using the TextFormat object in ActionScript.

On the CD-ROM 

You can find the completed htmlText_dynamic.fla document in the ch30 folder of this book's CD-ROM.

Formatting Fields with the TextFormat Object

ActionScript offers a wide range of options for the TextField object. Many properties of the MovieClip object, such as _x and _y, can be controlled with TextField objects, too. One interesting method of the TextField object is the setTextFormat() method. This method enables you to control the formatting of text within a TextField object without relying on HTML tags. The setTextFormat() method works much like the setTransform() method of the Sound class that we cover in Chapter 27, "Interacting with Movie Clips." Like setTransform(), setTextFormat() requires another object — a TextFormat object — that contains individual properties and values in order to adjust the targeted instance's text contents.

Tip 

ActionScript in Flash Player 6 and higher movies can also create text fields on the fly using the createTextField() method of the MovieClip object.

In this section, you create a Flash movie that makes a dynamic text field on the fly and applies formatting to the field with the TextFormat object. You also use the TextFormat object to determine the dimensions of the TextField instance.

  1. Create a new Flash document (Ctrl+N or z+N). Save the document as textFormat_100.fla.

  2. Rename Layer 1 to actions. ActionScript creates the entire contents of this movie. You will not use any tools from the Tools panel to create the text.

  3. Select frame 1 of the actions layer and open the Actions panel. Type the following code into the Script pane:

     var tfTitle:TextFormat = new TextFormat(); with(tfTitle){    font = "Verdana";     bold = true;     color = 0x0000FF;     url = "http://www.flashsupport.com/";     target = "_blank"; } 

    This code creates a new TextFormat object named tfTitle (line 1). This formatting is used for site links displayed in the Flash movie. Within this object, properties of the site link text are specified. Lines 2 through 8 assign the font, bold, color, url, and target properties of the style. In later steps, you apply this style to a TextField instance.

  4. Within the same actions list shown in Step 3, add the following code:

     var sName_1:String = "FlashSupport.com"; var oSize:Object = tfTitle.getTextExtent(sName_1); 

    This code declares a sName_1 variable, which contains the text of the site link that the movie displays. oSize is an object created by the getTextExtent() method of the TextFormat object. This method enables you to determine the pixel dimensions of text with formatting applied to it before you actually create the TextField object displaying the text. oSize contains two properties (among others): textFieldWidth and textFieldHeight. You access these properties in the next step.

    Tip 

    The TextFormat.getTextExtent() method has been greatly improved for Flash Player 7 and higher movies. The following properties are returned by the method: descent, ascent, textFieldHeight, textFieldWidth, height, and width. Refer to the Flash 8 Help panel for more information on these properties or read the coverage in the Flash ActionScript Bible series (Wiley). Oddly, Macromedia lists the getTextExtent() method as deprecated in the Flash 8 Help panel, but does not make any reference to a suitable replacement for its functionality. In our tests, the getTextExtent() method works reliably within Flash Player 8 movies.

  5. After the last line of code listed in Step 4, add the following code. Note that the image from book character indicates a continuation of the same line of code:

     var tSites:TextField = createTextField("tSites", 1, 10, 10, image from book    oSize.textFieldWidth, oSize.textFieldHeight); with(tSites){    html = true;    text = sName_1;    setTextFormat(tfTitle); } 

    The first line of code uses the createTextField() method of the MovieClip object to make a new Dynamic text field named tSites at a depth of 1 on the current timeline. The field is positioned at the X and Y coordinates of 10, 10, with a width and height specified by the oSize object created earlier.

    The second line begins a with() statement, to apply all of the nested code to the new tSites object.

    The third line enables the html property of the tSites field, so that any URL values within the TextFormat object can be recognized.

    The fourth line fills the text field with the value of the sName_1 variable, which has a value of "FlashSupport.com."

    The fifth line applies the tfTitle TextFormat object to the entire contents of the tSites field.

    Note 

    Make sure you use the setTextFormat() method if you want to apply formatting to existing text in a TextField instance. If you want to apply formatting before you populate a field with text, then use the setNewTextFormat() method.

  6. Save the Flash document as textFormat_100.fla and test it (Ctrl+Enter or z+Return). The tSites text field appears in the top-left corner of the Stage. When you click the text, the default Web browser opens, displaying the URL for www.flashsupport.com.

On the CD-ROM 

You can find the completed textFormat_100.fla file in the ch30 folder of this book's CD-ROM.

You can continue to develop this Flash document, adding more sites to the tSites text field. You don't necessarily need to make more text fields — create more sName variables (for example, sName_2, sName_3, and so on) and add their expressions together as the contents of the tSites instance.

Applying Style Sheets to Text Fields

In this section, you learn how to load an external style sheet, as a Cascading Style Sheet (CSS) file, and apply its styles to text in a Flash movie. This capability is available in Flash Player 7 and higher movies.

Making the Style Sheet

Before you can apply an external style sheet to Flash text, you need to create the CSS file describing the styles. In the following steps, use any text editor such as Notepad or TextEdit, or a full-featured CSS editor such as Macromedia Dreamweaver.

  1. Create a new text document.

  2. Define two custom styles, h1 and h2. These styles should use bold 14- and 12-pixel type, respectively. The font face should be Verdana for both. Refer to Listing 30-1.

    Listing 30-1: The h1, h2, and Body Styles

    image from book
     .h1 {    font-family: Verdana;    font-weight: bold;    font-size: 14px; } .h2 {    font-family: Verdana;    font-weight: bold;    font-size: 12px; } p {    font-family: Arial;    font-size: 10px;    color: #666666; } 
    image from book

  3. Now, define a p style. This style will be used for regular text in paragraphs. This style will use Arial at 10 px. The color of this text will be dark gray, using the hexadecimal value #666666. Refer to Listing 30-1.

  4. Save your text file as styles.css.

Loading the Style Sheet Into a Flash Movie

Once you have a CSS file ready, you can create the ActionScript required to load the style sheet into a Flash movie. In the following steps, you'll do just that.

  1. Create a new Flash document (File ð New).

  2. Rename Layer 1 to actions.

  3. Select frame 1 of the actions layer, and open the Actions panel (F9, or Option+F9 on Mac). Type the code shown in Listing 30-2 into the Script pane.

    Listing 30-2: The movieStyle Object

    image from book
     1.  var ssMovie:TextField.StyleSheet = new TextField.StyleSheet(); 2.  ssMovie.onLoad = function(bSuccess:Boolean):Void { 3.     if(bSuccess){ 4.        trace("loaded external CSS file"); 5.        showText(); 6.     } else { 7.        trace("CSS file did not load."); 8.     } 9.  }; 10. ssMovie.load("styles.css"); 
    image from book

    In line 1 of this code, you create a new StyleSheet object, which is actually a subclass of the TextField class. The StyleSheet object is named movieStyle.

    In line 2, you define the onLoad() method of this object. The onLoad() handler is invoked when the external CSS file has finished loading into the movie. If the loading was successful, lines 3 through 5 are invoked, sending a trace() message to the Output panel and invoking another function named showText(). You define the showText() function in the next section.

    If the CSS style cannot be loaded (that is, if the URL or filename is incorrect, or if the style sheet cannot be parsed by the Flash Player), lines 6 through 8 are invoked in the onLoad() handler. Line 7 sends a message indicating the result to the Output panel.

    Line 10 invokes the load() method of the movieStyle object. This line of code tells the Flash Player to get the styles.css file and attempt to load it.

    Tip 

    It's always a good idea to define the onLoad() handler of an object before you initiate the load() method. There's a slight chance that the onLoad() handler won't be invoked if you declare it after the load() method.

  4. Save your Flash document as css_styles_100.fla, in the same location where you saved your styles.css file from the previous section. Test the movie (Ctrl+Enter or z+Enter). You should see the following text appear in the Output panel:

     loaded external CSS file 

Applying the Styles to Flash Text

Once you have the external style sheet loaded into a Flash movie, you're ready to apply the defined styles to text used in dynamic or input text fields. In the remaining steps, you learn how to apply each of the styles to text displayed in a dynamic text field.

  1. In the Flash document that you created in the previous section, go back to frame 1 of the actions layer. Open the Actions panel (F9, or Option+F9 on Mac), and add the code shown in Listing 30-3 after the existing code. Note that the image from book character indicates a continuation of the same line of code. Do not type this character into your actual code.

    Listing 30-3: The showText() function

    image from book
     1.  var tDisplay:TextField; 2. 3.  function showText():Void { 4.     var sText:String = "<span class='h1'>Flash Player Style image from book           Support</span><br/>"; 5.     sText += "<p>You can use style sheets in your Flash image from book           Player 7 or higher movies.</p><br/>"; 6.     sText += "<span class='h2'>Supported styles</span><br/>"; 7.     sText += "<p>More text here   </p>"; 8.     tDisplay = createTextField("tDisplay", 1, 10, 10, 400, 400); 9.     with(tDisplay){ 10.       styleSheet = ssMovie; 11.       html = true; 12.       multiline = true; 13.       wordWrap = true; 14.       htmlText =sText; 15.    } 16. } 
    image from book

    Line 1 establishes a variable named tDisplay. This variable will be used as a TextField instance later in the showText() function.

    Line 3 declares the function's name, showText. Remember that in Listing 30-2, the showText() function is invoked in the ssMovie.onLoad() handler.

    Lines 4 through 7 create a long string of text that will be inserted into the tDisplay field. Here, you can see that each of the styles is used, defined in the class attributes of various <span> tags. Since you defined a style for the p tag in the style sheet, that style is automatically applied to any text enclosed by <p></p> tags in HTML text. In line 14, the htmlText property of the tDisplay instance is set to the value of the sText variable.

    Line 8 creates a text field named tDisplay. Line 10 sets the styleSheet property of the tDisplay instance to ssMovie. Lines 11 through 13 set up basic properties of the tDisplay instance, enabling it to display HTML text (line 11), and display wrapping text over multiple lines (lines 12 and 13).

  2. Save your Flash document, and test it. You should see nicely formatted text, specified by the details of the style sheet, as shown in Figure 30-3.

image from book
Figure 30-3: The formatted text

On the CD-ROM 

You can find the completed files, styles.css and css_styles_100.fla, in the ch30 folder of this book's CD-ROM.

You can also use other nonsystem fonts in style sheets, but you'll need to make sure that you have created linked Font symbols in your movie's library or that you have created empty text fields that include the embedded fonts.

Inserting Images into Text Fields

In this section, you learn how to load a JPEG image into a TextField object. Continue using the same Flash document you created in the last section.

On the CD-ROM 

Open the css_styles_100.fla document from the ch30 folder of this book's CD-ROM if you didn't complete the previous section.

  1. In the css_styles_100.fla document, select frame 1 of the actions layer, and open the Actions panel (F9, or Option+F9). Change the script to the code shown in Listing 30-4. Remember that you shouldn't include the image from book character in your actual code. This character denotes a continuation of the same line of code.

    Listing 30-4: The Modified showText() Function

    image from book
     #include "bio.as" var ssMovie:TextField.StyleSheet = new TextField.StyleSheet(); ssMovie.onLoad = function(bSuccess:Boolean):Void {    if(bSuccess){       trace("loaded external CSS file");       showText();    } else {       trace("CSS file did not load.");    } }; ssMovie.load("styles.css"); var tDisplay:TextField; function showText():Void {    var sText:String = "<img image from book       src='http://www.flashsupport.com/bio/portrait.jpg' image from book       id='mcPortrait' width='200' height='200' vspace='4' hspace='8' image from book       align='left'>";    sText += "<span class='h1'>Robert Reinhardt</span><br/>";    sText += sBio;    tDisplay = createTextField("tDisplay", 1, 10, 10, 400, 400);    with(tDisplay){       styleSheet = ssMovie;       html = true;       multiline = true;       wordWrap = true;       htmlText =sText;    } } 
    image from book

    This code uses the same structure of the showText() function from the last section. However, in line 1, you include the bio.as file, which you can find in the ch30 folder of this book's CD-ROM. A copy of this file should exist in the same location as your .fla file. The bio.as file declares a string variable named sBio, containing a sample biography.

    Tip 

    For a final production version of this Flash movie, you may want to load the data stored in the bio.as into the Flash movie at run time. For example, the biography text could be stored in an XML document and loaded with an XML object, or it could be loaded from a server-side script. For more information on data loading, read Chapter 29, "Sending Data In and Out of Flash."

    Within the showText() function, you use the <img> tag to include a JPEG image, portrait.jpg, in the tDisplay field. You can find this JPEG file in the ch30 folder of this book's CD-ROM, and it's also located at the URL shown in the function.

  2. Save your Flash document as img_insert_100.fla, and test it. When the movie loads, you should see the header and biography text wrap around the JPEG image, as shown in Figure 30-4.

image from book
Figure 30-4: The JPEG image with wrapping text

On the CD-ROM 

You can find the completed document, img_insert_100.fla, in the ch30 folder of this book's CD-ROM. Another version of this document, img_insert_101.fla, features a drawBorder() function, which dynamically draws a border around the JPEG image. The checkLoad() function within this file is similar to the functions you created in Chapter 28, "Sharing and Loading Assets."

You can also use symbol linkage identifier names as the value of the src attribute of the <img> tag. However, you may find that if you use several <img> tags in a text field, not all of the images load and/or display. In some of our experiments, we noticed that text realigned itself when text with URLs was rolled over. Be sure to test your Flash movies with the latest Flash Player 8 revisions, as Macromedia releases them.

Using Asfunction in Anchor Tags

Not only can you use HTML formatting in Flash text; but you can also execute Flash actions from your text fields using the <a> tag and a href attribute value of asfunction:function, argument. For example, if you wanted to link text to a function that loads a new Flash movie file (.swf) into a Movie Clip target, you can create a custom function that uses the loadMovie() action and reference that action from your <a href> tag for a text field. See the following code (note that the image from book indicates a continuation of the same line of code; do not insert this character into your actual code):

 var mcHolder:MovieClip; var tDisplay:TextField; function getSWF(sPath:String):Void{   mcHolder.loadMovie(sPath); } tDisplay.htmlText = "<a href=\" asfunction:getSWF,movie.swf\">Click to load image from book   movie</a>"; 

In this code example, the text within the <a></a> tags executes the getSWF() function, passing the string movie.swf as the sPath argument.

Tip 

The asfunction can only pass one string value. You do not need to enclose the argument in quotes. If you need to pass another ActionScript variable for the value, use the + operator to add it to the HTML text string.

If you need to pass more than one argument, you will need to send all the values as one string separated by a comma (or preferred character). Then you would use the String.split() method as follows (note that the image from book indicates a continuation of the same line of code; do not insert this character into your actual code):

 var mcHolder:MovieClip; var tDisplay:TextField; function getSWF(sArgs:String):Void{    var aParams:Array = sArgs.split(",");    var mc:MovieClip = eval(aParams[0]);    var sPath:String = aParams[1];    mc.loadMovie(sPath); } var sParams:String = "mcHolder,movie.swf"; tDisplay.htmlText = "<a href=\"asfunction:getSWF," + sParams + "\"> image from book   Click to load movie</a>"; 

In this example, the getSWF() function takes the sArgs argument and creates an array with the split() method. This array's name is aParams. The elements of the aParams array are the two string values separated by a comma in the sParams variable.

On the CD-ROM 

You can see examples of HTML-formatted Flash text and the asfunction in the ch30/asfunction folder of this book's CD-ROM.




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

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