17 October 2011
The article is designed for all levels of Flash users working with files containing TLF text. General knowledge of the Flash workspace and a basic understanding of working with ActionScript 3 is required.
Intermediate
Adobe Flash Professional CS5 introduced a new type of text field to Flash called TLF text. TLF text has several advantages over Classic text, such as advanced formatting options, better font compression and rendering, and support for complexly formatted languages. In order to support TLF text, Adobe Flash Player was updated to change how it loads the related runtime shared libraries (RSLs) and the content you define in the SWF file.
In this article you'll learn about the changes to Flash Player and the best options for publishing files containing TLF text. You'll review key concepts and then follow the instructions to build a sample project to help you visualize how it all fits together. The steps guide you through creating a simple modular banner using a loader SWF file and content SWF files, each of which contains TLF text (click Figure 1 to see an example). Along the way, you'll learn how to build a custom RSL preloader, correctly load content SWF files, and work with ActionScript and FlashVars across multiple SWF files.
Special thanks to Jeff Kamerer at Adobe Systems for his help and suggestions. Be sure to check out Jeff's blog post, Using a custom preloader loop with TLF text, for more related information.
For best results in your projects that use TLF text, it's important to have a solid understanding of what TLF text is and how it works in Flash Player. This section covers key concepts related to TLF text and runtime shared libraries.
The Text Layout Framework (TLF) is a new and more powerful option for displaying text content in Flash. You can use the Property inspector to choose the TLF Text option in Flash while the Text tool is selected (see Figure 2).
TLF text is an authortime representation of the Text Layout Framework (TLF), which is an ActionScript 3 library built on top of the Flash Text Engine (FTE) classes. Essentially it's an extension of the core ActionScript language which adds a wide range of features to the text rendering engine.
New text features include the following:
The fact that TLF is an ActionScript framework "under the hood" means that you can work with TLF text in both a visual mode during authortime in Flash or in ActionScript at runtime. Authortime TLF text provides an enhanced visual interface for working with TLF (see Figure 3).
Flash supplies the TLF library at authortime. A runtime shared library is used to supply the functionality at runtime.
For more information on TLF text basics, see the following resources:
The TLF ActionScript classes are distributed in the form of a runtime shared library (RSL) contained in a SWZ file. By using an external RSL for the ActionScript library, the RSL can be cached locally on end users' machines and reused by all subsequent SWF files. In order for Flash Player to display TLF text, it first tries to load the RSL from the user's local cache, then from adobe.com, and then finally looks for it on the server hosting the SWF file. The RSL is always loaded in Flash Player before the content loads. This can create a delay if the RSL isn't already cached on the user's local computer.
You basically have four options for handling the potential delay of RSL loading. Three of these options involve using a preloader assigned to the file in the Advanced ActionScript 3 Settings (see Figure 4).
Selecting the runtime shared library (RSL) option from the Default linkage menu enables you to choose the preloader options from the fields below it. Your options include the following:
Tip: The source files for the Adobe preloader SWF are located in the Flash application folder on your computer (Adobe Flash CS5.5/Common/Configuration/ActionScript 3.0/rsls/).
Most people probably use the default Adobe preloader option without ever knowing it is there. For most use cases, that's fine, but there are a few exceptions.
The following are considered best practices for working with RSLs:
Preloading RSLs introduces a new timing scheme to control how your Flash content loads in Flash Player. This is generally not a problem but it does create some issues with loading SWF files inside other SWF files, like in a banner system or other kind of modular application.
The primary issues when loading a SWF with an ActionScript 3 Loader object are as follows:
Not to worry—these issues are easy to work around if you know how to do it.
Flash Professional CS5.5 introduces a handful of new ActionScript APIs to handle the needs of SWFs loading RSLs. The new ProLoader and ProLoaderInfo classes should be used as an alternative to the Loader and LoaderInfo classes when loading SWF files containing TLF. The ProLoader and ProLoaderInfo classes provide the same API and functionality as the Loader and LoaderInfo classes, but the Proloader has the ability to manage the preloading of the RSLs and provides the expected behavior when cross-scripting and working with events. The ProLoader is built to make these issues transparent to the end user—and when loading SWF files across security domains, the ProLoader minimizes the occurrences of similar issues. The LoaderContext class was also updated to support the ProLoader class. You'll work with the new parameters property in the next section of this article to learn a convenient way to pass FlashVars parameters to the root timeline of any loaded SWF.
The new ActionScript APIs require Flash Player 10.2 or later to function properly.
For more information on working with the ProLoader class, see Using the ProLoader and ProLoaderInfo classes in the ActionScript 3 Developer's Guide.
If you're still working in Flash Professional CS5, the best strategies involve merging the RSL into the SWF to sidestep the Loader object issues or using the SafeLoader sample files provided on the following TechNote: Loading child SWFs with TLF content generates errors.
In this section you'll learn how to work with RSL preloaders and Flash Professional CS5.5 ActionScript in a banner structure. The banner is a very simple application composed of a loader SWF file and multiple child content SWF files. The loader SWF file loads the content SWF based on a URL supplied in FlashVars, passes the content SWF a reference to the FlashVars for text content, and then calls a transition method on the SWF to fade the content into view. Along the way you'll explore what works and what doesn't work when using TLF text.
There are several key concepts you'll focus on while creating the source files:
parameters property of the LoaderContext class to pass FlashVars parameters to the child SWF filesFirst you'll prepare the source files that make up the sample project. You can use the supplied images in the ZIP file to follow along with the steps below.
By the end of the project, you'll create the following source files:
Follow these steps to get started:
You'll work on the FlashVars and ActionScript code in the next sections, but first you'll start by creating the FLA files.
Since you're building a web-based project, you'll create a custom RSL preloader for this exercise. The custom preloader animation can be built in two different ways:
For this example, you'll create a separate SWF file as a demonstration of the easiest way to customize the animation (see Figure 5).
Follow these steps to set up your custom preloader FLA file:
gotoAndPlay("loop") action on the last frame to allow the animation to loop without returning to Frame 1.Tip: Be sure to leave 15 frames of empty space on the Timeline before the animation appears. Otherwise you'll see the animation appear briefly when your SWF file loads.
The banner SWF file acts as a loader for the content SWF files. This file is empty except for a TLF text field showing a title. Beyond loading the content SWF files, the banner SWF file can be used to hold any text, content, or functionality that would be used repeatedly across content SWF files. In this case, the title text will appear the same way across any content SWF file loaded. You could also place user interface controls (such as navigation buttons) in the banner FLA or shared functionality like click through areas.
Follow these steps to set up the banner FLA:
Tip: The loader SWF file can share its embedded fonts with the content SWF file. That means that you don't have to embed the Arial Bold again in each SWF file.
Follow these steps to assign the custom preloader SWF file to the banner SWF file:
In the last set of steps, you simply assigned your CustomPreloader.swf file in place of the default Adobe SWF file.
The content FLA files are very simple. They contain a background image and a large TLF text field that gets populated with data supplied in the FlashVars parameters. For simplicity's sake, the images will be scenic pictures and the text will be the name of the location (see Figure 6).
Follow these steps to set up the content FLA files:
Tip: You can set the RSL preloader preferences for the child SWF files too, but theoretically you wouldn't expect a delay since the parent SWF file has already loaded the RSL.
At this point you've created all of the FLA files that will be used in the project. Next you'll set up the HTML page.
The FlashVars parameter is an optional tag that can be added to the HTML markup that embeds the SWF file on the page. You can use it to pass variables to the SWF file at startup, similar to the way URL string variables can be appended to a URL to pass variables to that URL.
The FlashVars parameter looks like this:
<param name="FlashVars" value="&firstname=Dan&lastname=Carr&" />
Notice that the value attribute contains a list of name value pairs separated by ampersands.
Using FlashVars to supply text and URLs to the SWF is a great way to make the application modular. If you're editing by hand, you can manually make changes in a text editor without republishing the SWF files. If you're writing your HTML dynamically, you can change the content and text on the fly on the server. By separating the text values from the content, you could localize the content to different languages or provide dynamic variations of the text per user, and other similar strategies.
Follow these steps to publish the HTML page:
Follow these steps to add the FlashVars parameters:
After making these changes, the object tags in Banner.html should look like this:
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="720" height="300" id="Banner" align="middle">
<param name="movie" value="Banner.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<param name="play" value="true" />
<param name="loop" value="true" />
<param name="wmode" value="window" />
<param name="scale" value="showall" />
<param name="menu" value="true" />
<param name="devicefont" value="false" />
<param name="salign" value="" />
<param name="allowScriptAccess" value="sameDomain" />
<param name="flashVars" value=" &page_url=Page1.swf&page_name=San Francisco, CA" />
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data=" Banner" width="720" height="300">
<param name="movie" value="Banner.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<param name="play" value="true" />
<param name="loop" value="true" />
<param name="wmode" value="window" />
<param name="scale" value="showall" />
<param name="menu" value="true" />
<param name="devicefont" value="false" />
<param name="salign" value="" />
<param name="allowScriptAccess" value="sameDomain" />
<param name="flashVars" value=" &page_url=Page1.swf&page_name=San Francisco, CA" />
For more information on FlashVars, see the following resources:
Using FlashVars with ActionScript 3 (Peter deHaan)
Use FlashVars to pass variables to SWF files
In this section you'll add functionality to the banner FLA by creating an ActionScript class and assigning it as the Document class of the banner.fla file. This is the equivalent of writing ActionScript code on Frame 1 of the main Timeline, but as you'll see when you get to the code for the content SWF files, you can reuse Document classes across FLA files.
In the next steps you'll create an ActionScript class called Banner.as that can do the following:
Tip: Review the completed files in the supplied banner_project_completed.zip file and use them as a reference as you work through the next sections of this tutorial.
Follow these steps to create the ActionScript class:
package
{
import fl.display.ProLoader;
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.LoaderContext;
public class Banner extends MovieClip
{
//--------------------
// Variables
public var pageLoader:ProLoader;
public var pageURL:String = "Page1.swf";
//--------------------
// Constructor
public function Banner()
{
// Get page_url FlashVars
var url:String = this.loaderInfo.parameters["page_url"];
if( url ){
pageURL = url;
}
}
// Pass the FlashVars to the ProLoader
var lc:LoaderContext = new LoaderContext();
lc.parameters = this.loaderInfo.parameters;
// Create the loader
pageLoader = new ProLoader();
pageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSWFLoaded);
pageLoader.load( new URLRequest(pageURL), lc );
addChild(pageLoader);
// Bring text to front
setChildIndex(state_txt, numChildren - 1);
//--------------------
// Events
private function onSWFLoaded( event:Event ):void
{
// Cross-script with child SWF
var content:MovieClip = pageLoader.content as MovieClip;
content.transition();
}
Tip: Notice that the code in Step 9 assigns the data types of the content SWF file to MovieClip instead of its Document class type, Page. Use this approach as your simplest solution when cross-scripting from one SWF file to another. The Document class for each SWF file includes auto-generated code for TLF support. If the loader SWF file imports the Page class for data typing, the loader SWF file's version of Page will be used in the content SWF files instead of their own version. The result is that the content SWF files don't have access to the auto-generated TLF code and therefore appear without text formatting.
Follow these steps to assign the Banner class to the Banner FLA file:
In this section you'll create a single Document class for all three child page SWF files. This demonstrates the benefits of using an ActionScript class instead of repeating the same code in each FLA. This workflow also opens up the possibilities of working on your scripts in Adobe Flash Builder or another ActionScript text editor.
In the next steps you'll create an ActionScript class called Page.as that can do the following:
Follow these steps to create the child page ActionScript class:
package
{
import fl.transitions.Tween;
import fl.transitions.easing.Strong;
import flash.display.MovieClip;
import flash.events.Event;
public class Page extends MovieClip
{
//--------------------
// Variables
public var pageTween:Tween;
public var pageName:Array = "San Francisco, CA";
//--------------------
// Constructor
public function Page()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
//--------------------
// Events
private function onAddedToStage( event:Event ):void
{
// Get page_names FlashVars passed to us from the ProLoader
var page_name:String = this.loaderInfo.parameters["page_name"];
if(page_name)
{
// Split the string to an array
pageName = page_name;
}
name_txt.text = pageName;
}
//--------------------
// Methods
// Fade in
public function transition():void
{
if( pageTween ) {
pageTween.stop();
}
pageTween = new Tween(this, "alpha", Strong.easeOut, 0, 1, 2, true);
}
That wraps it up for the development tasks to finish the project. Check your work against the completed sample files for accuracy.
For more information on working with ActionScript 3, refer to the following resources:
The last task of the tutorial is to test your work. In a real-world workflow it is important to perform tests along the way many times as you change the files. And be sure to test often when writing ActionScript. It's much easier to identify where issues are, when they occur.
You can use the following approaches for testing the project:
Tip: You can also use the advanced features of Flash Builder to edit and debug the ActionScript code.
The Flash Debugger can be used to find problems in ActionScript when errors occur. When you test the file in the Debugger, it will pause at the point where the code breaks and show you the state of the variables and function call stack. This provides a very handy way of seeing problems that are otherwise difficult to pin point.
Follow these steps to test the project in the debugger:
For more information on debugging ActionScript 3, read my article, Understanding ActionScript 3 debugging in Flash.
The Bandwidth Profiler is a utility attached to the SWF window when you publish the SWF using the Control > Test Movie command (see Figure 8).
Follow these steps to test the RSL preloader animation:
You can also test the project in a browser to see the FlashVars parameters load the content instead of the default parameters.
Follow these steps to test in a browser:
Tip: If you see an ActionScript runtime error when you launch the SWF files from your desktop or within a browser, it may be that you don't have Flash Player 10.2 or later installed on your computer. Make sure you have the latest Flash Player version installed before testing.
That's it! You now have a simple modular banner structure capable of dynamically loading content SWF files, passing data to them, and cross-scripting with them.
The sample project described in this tutorial demonstrates how to work with TLF RSLs in Flash Professional CS5.5. It also provides the foundation structure for building modular Flash applications. You could take it a step further by adding localization features (after all, TLF is designed to render languages that were not possible using Classic text). Take a look at my article, Formatting text for localized Flash projects, for more information.
Another way you could extend the sample project involves adding a custom preloader loop animation to Banner.swf in place of the external preloader SWF file. The benefit of this approach is that you can assign event handlers to the main Timeline of the banner SWF file and show a progress bar animation instead of an indeterminate animation.
To research further, check out Using a custom preloader loop with TLF text by Jeff Kamerer.
Here is also some Flash documentation related to TLF and RSLs:
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.
| 04/23/2012 | Auto-Save and Auto-Recovery |
|---|---|
| 04/23/2012 | Open hyperlinks in new window/tab/pop-up ? |
| 04/21/2012 | PNG transparencies glitched |
| 04/01/2010 | Workaround for JSFL shape selection bug? |
| 02/13/2012 | Randomize an array |
|---|---|
| 02/11/2012 | How to create a Facebook fan page with Flash |
| 02/08/2012 | Digital Clock |
| 01/18/2012 | Recording webcam video & audio in a flv file on local drive |