O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example: Sprite Factory

ActionScript 3.0 introduced the Sprite class, which is a lightweight building block for interactive objects on stage. MovieClips are now derived from the new Sprite class. The factory method pattern can come in handy when developing applications that utilize sprites in Flash. Sprites are frequently added, and their behavior and appearance modified, during the course of Flash application development. Therefore, managing the dependencies between sprites and the rest of the application can be advantageous. We will create a simple example application called Shapes that manages sprite creation by introducing a factory method that enables clients to create sprites without explicitly specifying their class names.

The application shown in Figure 2-8 will simply draw four different shapes based on the Sprite class on the Flash stage. The first set of shapes will consist of an unfilled rectangle and circle. The second set will be a filled rectangle and circle. The example is not a productive application, but it will serve as a springboard to the vertical shooter game that we'll develop later in the chapter.

Screenshot of Shapes example stage

Figure 2-8. Screenshot of Shapes example stage

The Project window in Figure 2-9 shows the file structure of the Shapes example.

Shapes example Project window

Figure 2-9. Shapes example Project window ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required