O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

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: An Object-Oriented Shape Library

The graphical content created by the built-in shape drawing methods (drawEllipse( ), drawRect( ), etc.) does not correspond to any object, and cannot be changed or removed independently once drawn. In ActionScript, there are no classes that give object-oriented access to corresponding onscreen shapes. This section shows one way to address that shortcoming, showing an example implementation of a small class library for creating and manipulating shapes as objects.

The classes in our example shape library are as follows: BasicShape, Rectangle, Ellipse, Star, and Polygon. BasicShape is the base class of the library. It extends the built-in Shape class, which provides a lightweight, basic surface on which to draw shapes. The BasicShape class manages stroke and fill styles for all shapes and determines when a shape needs to be drawn. Instances of the remaining classes represent geometric shapes that can be added to, and removed from, the display list. Each shape class implements its own specific drawing routine. Once a shape object is created, its stroke, fill, and outline can be updated freely.

The following six examples put many of the techniques we've studied in this book into practice—especially those covered in this chapter. Pay close attention to the numerous comments; they will guide you through the code.

Example 25-1 shows the BasicShape class, an abstract-style class that defines the basic functionality of all shapes in the class library. ...

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