Object-oriented ActionScript - An Example Website
(Page 7 of 7 )
Most real-world Flash applications lie somewhere between the extreme poles of timeline-only development and pure OOP development. For example, consider a Flash-based web site in which two buttons slide into the center of the screen and offer the user a choice of languages: “English” or “French.” The user clicks the preferred language button, and both buttons slide off screen. An animated sequence then displays company information and a video showing a product demo. The video is controlled by a MediaPlayback component.
Our hypothetical web site includes both procedural programming and OOP, as follows:
- Frames 2 and 3 contain preloader code.
- Frame 10 contains code to start the button-slide animation.
- Frames 11–25 contain the button-slide animation.
- Frame 25 contains code to define button event handlers, which load a language-specific movie.
- In the loaded language-specific movie, frame 1 contains code to control the MediaPlayback component.
In the preceding example, code placed directly on frames (e.g., the preloader code) is procedural. But the buttons and MediaPlayback component are objects derived from classes stored in external .as files. Controlling them requires object-oriented programming. And, interestingly enough, Flash components are, themselves, movie clips. Movie clips, so intrinsic to Flash, can be thought of as self-contained objects with their own timelines and frames. Components (indeed, any movie clip) can contain procedural code internally on their own frames even though they are objects. Such is the nature of Flash development—assets containing procedural code can be mixed on multiple levels with object-oriented code.
Note: As mentioned in the Preface, this bookassumes you understand movie clips and have used them in your work. If you are a programmer coming to Flash from another language, and you need a crash course on movie clips from a programmer’s perspective, consult Chapter 13 of ActionScript for Flash MX: The Definitive Guide (O’Reilly), available online at http://moock.org/asdg/samples.
Flash’s ability to combine procedural and object-oriented code in a graphical, time-based development environment makes it uniquely flexible. That flexibility is both powerful and dangerous. On one hand, animations and interface transitions that are trivial in Flash might require hours of custom coding in languages such as C++ or Java. But on the other hand, code that is attached to frames on timelines or components on the Stage is time-consuming to find and modify. So overuse of timeline code in Flash can quickly (and quietly!) turn a project into an unmaintainable mess. Object-oriented techniques stress separation of code from assets such as graphics and sound, allowing an object-oriented application to be changed, reused, and expanded on more easily than a comparable timeline-based program. If you find yourself in the middle of a timeline-based project faced with a change and dreading the workinvolved, chances are the project should have been developed with object-oriented principles from the outset. Although OOP may appear to require additional up-front development time, for most nontrivial projects, you’ll recoup that time investment many times over later in the project.
Ultimately, the amount of OOP you end up using in your workis a personal decision that will vary according to your experience and project requirements. You can use the following list to help decide when to use OOP and when to use procedural timeline code. Bear in mind, however, that these are just guidelines—there’s always more than one way to create an application. Ultimately, if the software works and can be maintained, you’re doing something right.
Consider using OOP when creating:
- Traditional desktop-style applications with few transitions and standardized user interfaces
- Applications that include server-side logic
- Functionality that is reused across multiple projects
- Components
- Games
- Highly customized user interfaces that include complex visual transitions
Consider using procedural programming when creating:
- Animations with small scripts that control flow or basic interactivity
- Simple applications such as a one-page product order form or a newsletter subscription form
- Highly customized user interfaces that include complex visual transitions
You’ll notice that the bulleted item “Highly customized user interfaces that include complex visual transitions” is included as a case in which you might use both OOP and procedural programming. Both disciplines can effectively create that kind of content. However, remember that OOP in Flash is typically more maintainable than timeline code and is easier to integrate into version control systems and other external production tools. If you suspect that your highly customized UI will be used for more than a single project, you should strongly consider developing it as a reusable class library or set of components with OOP.
Note that in addition to Flash’s traditional timeline metaphor, Flash MX Professional 2004 introduced a Screens feature (which includes both Slides and Forms). Screens provide a facade over the traditional timeline metaphor. Slides and Forms are akin to the card-based metaphors of programs like HyperCard. Slides are intended for PowerPoint-style slide presentations, while Forms are intended for VB developers used to working on multipage forms. Like timeline-based applications, Screens-based applications include both object-oriented code (i.e., code in classes) and procedural-style code (i.e., code placed visually on components and on the Screens of the application). As mentioned in the Preface, this bookdoes not cover Screens in detail, but the foundational OOP skills you’ll learn in this text will more than equip you for your own exploration of Screens.
On with the Show! In this chapter, we summarized the core concepts of OOP in Flash. We’re now ready to move on with the rest of Part I, where we’ll study all of those concepts again in detail, applying them to practical situations along the way. If you’re already quite comfortable with OOP and want to dive into some examples, see Chapters 5, 7, 11, and 12, and all of Part III, which contain in-depth studies of real-world object-oriented code.
Let’s get started!
 | 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. |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |