Object-oriented ActionScript - Starting an Objected-Oriented Application
(Page 6 of 7 )
In our brief overview of OOP in Flash, we’ve seen that an object-oriented application is made up of classes and objects. But we haven’t learned how to actually start the application running. Every Flash application, no matter how many classes or external assets it contains, starts life as a single .swf file loaded into the Flash Player. When the Flash Player loads a new .swf file, it executes the actions on frame 1 and then displays the contents of frame 1.
Hence, in the simplest case, we can create an object-oriented Flash application and start it as follows:
- Create one or more classes in .as files.
- Create a .fla file.
- On frame 1 of the .fla file, add code that creates an object of a class.
- Optionally invoke a method on the object to start the application.
- Export a .swf file from the .fla file.
- Load the .swf file into the Flash Player.
We’ll study more complex ways to structure and run object-oriented Flash applications in Chapters 5, 11, and 12.
But How Do I Apply OOP? Many people learn the basics of OOP only to say, “I understand the terminology and concepts, but I have no idea how or when to use them.” If you have that feeling, don’t worry, it’s perfectly normal; in fact, it means you’re ready to move on to the next phase of your learning—object-oriented design (OOD).
The core concepts of OOP (classes, objects, methods, properties, etc.) are only tools. The real challenge is designing what you want to build with those tools. Once you understand a hammer, nails, and wood, you still have to draw a blueprint before you can actually build a fence, a room, or a chair. Object-oriented design is the “draw a blueprint” phase of object-oriented programming, during which you organize your entire application as a series of classes. Breaking up a program into classes is a fundamental design problem that you’ll face daily in your OOP work. We’ll return to this important aspect of OOP regularly throughout this book.
But not all Flash applications need to be purely object-oriented. Flash supports both procedural and object-oriented programming and allows you to combine both approaches in a single Flash movie. In some cases, it’s sensible to apply OOP only to a single part of your project. Perhaps you’re building a web site with a section that displays photographs. You don’t have to make the whole site object-oriented; you can just use OOP to create the photograph-display module. (In fact, we’ll do just that in Chapters 5 and 7!)
So if Flash supports both procedural and object-oriented programming, how much of each is right for your project? To best answer that question, we first need to understand the basic structure of every Flash movie. The fundamental organizing structure of a Flash document (a .fla file) is the timeline, which contains one or more frames. Each frame defines the content that is displayed on the graphical canvas called the Stage. In the Flash Player, frames are displayed one at a time in a linear sequence, producing an animated effect—exactly like the frames in a filmstrip.
At one end of the development spectrum, Flash’s timeline is often used for interactive animation and motion graphics. In this development style, code is mixed directly with timeline frames and graphical content. For example, a movie might display a 25-frame animation, then stop, calculate some random feature used to display another animation, and then stop again and askthe user to fill in a form while yet another animation plays in the background. That is, for simple applications, different frames in the timeline can be used to represent different program states (each state is simply one of the possible places, either physical or conceptual, that a user can be in the program). For example, one frame might represent the welcome screen, another frame might represent the data entry screen, a third frame might represent an error screen or exit screen, and so on. Of course, if the application includes animation, each program state might be represented by a range of frames instead of a single frame. For example, the welcome screen might include a looping animation.
When developing content that is heavily dependent on motion graphics, using the timeline makes sense because it allows for precise, visual control over graphic elements. In this style of development, code is commonly attached to the frames of the timeline using the Actions panel (F9). The code on a frame is executed immediately before the frame’s content is displayed. Code can also be attached directly to the graphical components on stage. For example, a button can contain code that governs what happens when it is clicked.
Timeline-based development usually goes hand-in-hand with procedural programming because you want to take certain actions at the time a particular frame is reached. In Flash, “procedural programming” means executing code, defining functions, and setting variables on frames in a document’s timeline or on graphical components on stage.
However, not all Flash content necessarily involves timeline-based motion. If you are creating a video game, it becomes impossible to position the monsters and the player’s character using the timeline. Likewise, you don’t know exactly when the user is going to shoot the monster or take some other action. Therefore, you must use ActionScript instead of the timeline to position the characters in response to user actions (or in response to an algorithm that controls the monsters in some semi-intelligent way). Instead of a timeline-based project containing predetermined animated sequences, we have a nonlinear project in which characters and their behavior are represented entirely in code.
This type of development lends itself naturally to objects that represent, say, the player’s character or the monsters. At this end of the development spectrum lies traditional object-oriented programming, in which an application exists as a group of classes. In a pure object-oriented Flash application, a .fla file might contain only a single frame, which simply loads the application’s main class and starts the application by invoking a method on that main class. Of course, OOP is good for more than just video games. For example, a Flash-based rental car reservation system might have no timeline code whatsoever and create all user interface elements from within classes.
 | If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!
Visit the O'Reilly Network http://www.oreillynet.com for more online content. |
Next: An Example Website >>
More Flash Articles
More By O'Reilly Media