| < Day Day Up > |
11.7 Let's See It in Action!
Although this chapter may have felt quite conceptual, it lays very important
|
| < Day Day Up > |
| < Day Day Up > |
Chapter 12. Using Components with ActionScript 2.0
In Chapter 11, we learned how to structure a basic OOP application in ActionScript 2.0. In this chapter, we'll see how to create a GUI application based on that structure. As usual, you can download the sample files discussed in this chapter from http://
|
| < Day Day Up > |
| < Day Day Up > |
12.1 Currency Converter Application Overview
Our example GUI application is a simple currency converter. Figure 12-1 shows the
Figure 12-1. The currency converter application
We'll place the assets for our application in the following directory structure, which mirrors the structure we used in Chapter 11. Note that the
deploy
and
source
folders are both subdirectories of
CurrencyConverter
and that
org/
CurrencyConverter/
deploy/
source/
org/
moock/
tools/
Our application's main Flash document is named
CurrencyConverter.fla
. It resides in
CurrencyConverter/source
. To create the
CurrencyConverter.fla
file, we'll copy the file
AppName.fla
(which we created in Chapter 11) to the
CurrencyConverter/source
directory, and rename the file to
CurrencyConverter.fla
. That gives
CurrencyConverter.fla
the basic structure it needs, including a class preloader on
Our application's only class is CurrencyConverter . It is stored in an external .as class file named CurrencyConverter.as , which resides in CurrencyConverter/source/org/moock/tools . Our exported application (a Flash movie) is named CurrencyConverter.swf . It resides in CurrencyConverter/deploy .
Now let's take a closer look at each of these
|
| < Day Day Up > |
| < Day Day Up > |
12.2 Preparing the Flash Document
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
12.2.1 Adding Components to the DocumentHere 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.
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.
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
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.
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
With our component-loading keyframe prepared, we can now place a dummy instance of each component on our document's timeline, as follows:
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
12.2.2 Starting the ApplicationIn 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
Our
CurrencyConverter.fla
file is now ready. We can now
|
| < Day Day Up > |