12.2 Preparing the Flash Document

 <  Day Day Up  >  

Our CurrencyConverter class instantiates all the components needed for our application at runtime. Even though we create instances of components at runtime, Flash requires us to add the components to the CurrencyConverter.fla 's Library during authoring. Unfortunately, Flash does not allow us to simply drag the required components from the Flash Components panel to CurrencyConverter.fla 's Library. Instead, to add a component to the Library, we must drag an instance of it to the Stage. Although component instances can be left on the Stage of a .fla file, that development style is not our current focus. So we'll delete the instances from the Stage; however, Flash leaves a copy of the underlying component in the Library (which was our original goal).

12.2.1 Adding Components to the Document

Here are the steps to follow to add the Button component to CurrencyConverter.fla 's Library. To add the ComboBox, Label, TextArea, and TextInput components to the Library, follow the same steps, choosing the appropriate component instead of Button.

  1. With CurrencyConverter.fla open in the Flash authoring tool, select Window Development Panels > Components.

  2. Select and delete the Button instance from the Stage.

If we were working in a brand new .fla file, our components would now be ready for runtime instantiation. However, recall that we're working with the basic Flash document we created in Chapter 11, which exports its classes at frame 10 and displays a preloading message while those classes load. Because of this preloading structure, our components would not currently work if we attempted to use them! We need to integrate the components into our preloading structure.

When a document's ActionScript 2.0 classes are exported on a frame later than frame 1, components will not work in that document unless they are loaded after the class export frame!


To load our components after frame 10, we must set them to not export on frame 1, and then we must place a dummy instance of each component on stage after frame 10. The dummy instance is not used; it merely forces the component to load.

Here are the steps we follow to prevent the Button component from being exported on frame 1. To prevent the remaining components from being exported on frame 1, follow the same steps, choosing the appropriate component instead of Button.

  1. Select the Button component in the Library.

  2. From the Library's pop-up Options menu in the top-right corner of the panel, select Linkage.

  3. In the Linkage Properties dialog box, under Linkage, uncheck the Export in First Frame checkbox.

  4. Click OK.

When a component's Export in First Frame option is unchecked, the component is not compiled with the movie unless an instance of the component is placed on the document's timeline. The component loads at the frame on which an instance of it is placed. But the component initialization process requires a movie's ActionScript 2.0 classes to be available before the component is exported. Hence, in our CurrencyConverter.fla document, we'll place an instance of each of our components on frame 12, two frames after our document's ActionScript 2.0 classes are exported. To store the dummy component instances, we'll create a new timeline layer and keyframe, as follows :

  1. Select Insert Timeline Layer.

  2. Select frame 12 in the load components layer.

  3. Select Insert Timeline Keyframe.

  4. Select Insert Timeline Keyframe. This second keyframe prevents the dummy component instances from showing up in our application. We need them only for loading; the CurrencyConverter class handles the actual creation of component instances in our application.

With our component-loading keyframe prepared, we can now place a dummy instance of each component on our document's timeline, as follows:

  1. Select frame 12 in the load components layer.

  2. From the Library, drag an instance of each component to the Stage.

  3. Optionally use the Component Inspector panel (Window Development Panels Component Inspector) to add dummy text to each component instance indicating that it is not used, as shown in Figure 12-2. Consult Flash's online help for instructions on setting component parameters via the Component Inspector.

Figure 12-2 shows how our document's timeline and Stage look with frame 12 of the load components layer selected.

Figure 12-2. CurrencyConverter.fla's timeline and Stage
figs/as2e_1202.gif

12.2.2 Starting the Application

In Chapter 11, we saw how to start an OOP application by invoking the main( ) method of the application's primary class (i.e., the class and method that we design to be the program launch point). We'll start our currency converter by invoking main( ) on our application's primary (indeed, only) class, CurrencyConverter . The main( ) call comes on frame 15 of the scripts layer in CurrencyConverter.fla 's timeline, after our classes and components have been preloaded. Here's the code:

 import org.moock.tools.CurrencyConverter; CurrencyConverter.main(this, 0, 150, 100); 

Notice that the import statement allows us, in the future, to refer to the CurrencyConverter class by its name without typing its fully qualified package path . As implied by the preceding code, our class's main( ) method expects four parameters: the movie clip to hold the currency converter ( this ) and the depth, horizontal position, and vertical position ”0, 150 and 100, respectively ”at which to display the converter within the clip.

Our CurrencyConverter.fla file is now ready. We can now turn our attention to the class that creates and manages the currency converter application itself, CurrencyConverter .

 <  Day Day Up  >  


Essential ActionScript 2.0
Essential ActionScript 2.0
ISBN: 0596006527
EAN: 2147483647
Year: 2004
Pages: 177
Authors: Colin Moock

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