ActionScript 3.0 Design Patterns

(Chris Devlin) #1

6 | Chapter 1: Object-Oriented Programming, Design Patterns, and ActionScript 3.0


Built-in State Machines: while most programmers welcomed the
demise of movie clip and button embedded scripts, one astute pro-
grammer observed that Flash used to have built-in state machines.
Jonathan Kaye, PhD, co-author ofFlash MX for Interactive Simula-
tion: How to Construct and Use Device Simulations(Delmar Learning,
2002), noted that the button and movie clip scripts often served to cre-
ate state machines. Each button or movie clip could serve as an encap-
sulated, context-sensitive trigger for changing state. (See how design
patterns deal with State Machines in Chapter 10.)

In general, the demise of movie clip and button scripts is seen as a boon to better


programming, especially OOP programming. Keeping track of the isolated button


and movie clip codes could be a headache, even with relatively small applications.


For structuring larger programs, where OOP and Design Patterns are most useful,


having movie clips and buttons floating around with their own code moves the prob-


lem from the realm of headache to nightmare. So, for learning design patterns, be


glad you don’t even have to think about little scripts isolated in movie clips and


buttons.


Timeline scripts


Another kind of scripting you’ll be seeing less of in Flash are those embedded in your


Timeline. For the most part, placing scripts in the Timeline probably left a lot to be


desired in the first place, but worked out to be a convenient location. In Action-


Script 2.0, you were able to place a script in a class and call it from a script embed-


ded in the Timeline, and so all that the Timeline code was really used for was to call


a class that would launch a script in an ActionScript file (.as). That being the case,


the Flash CS3.flafile has a little window where you can add the name of the class to


call. (See the next section.) So, if all you want to do is to call a program and compile


it into an SWF file, you no longer need to use the Timeline for your code at all.


However, Flash CS3 doesn’t do away with code in the Timeline. You can still use it,


but in this book, we use it selectively only with movie clips that are called from a


class outside the movie clip or button class. (See the section “Movie clip and button


classes.”)


Document class


You won’t be placing much, if any, code in the Timeline using ActionScript 3.0.


Rather than using an object with a Timeline script, you can now compile your.as


files by entering the name of the class name you want to launch your application.


Figure 1-1 shows how to use the Document class window in the Properties panel to


enter the name of the class you want to launch:


You can still use the Timeline, but unless there’s a good reason to do so, there’s no


need. Most of the examples in this book use theSpriteobject instead of the

Free download pdf