Chapter 7. Drawing and Masking

Introduction

With ActionScript, you can programmatically draw many display objects such as Shape, Sprite, Button, and MovieClip. Each of these classes has a graphics property that is an instance of the flash.display.Graphics class. The Graphics class defines an API for drawing content programmatically. Most recipes in this chapter discuss how to use the Graphics class API.

Since the Shape, Sprite, Button, and MovieClip classes already define graphics properties that are references to Graphics instances, it is not necessary to construct new Graphics objects. The graphics property for a display object draws within that display object. For example, the following code sets the line style for the Graphics object targeting a sprite called sampleSprite:

sampleSprite.graphics.lineStyle();

The Graphics class defines an API for drawing basic lines and simple shapes. However, some common shapes are difficult to draw with the Graphics API. For that reason, the AS3CBLibrary (available at http://www.rightactionscript.com/ascb) includes an ascb.drawing.Pen class. The Pen class is a proxy (a wrapper) for the Graphics class. You can construct a new Pen instance by passing it a reference to the Graphics object you want to target:

var pen:Pen = new Pen(sampleSprite.graphics);

The Pen class proxies requests to all the methods of the Graphics class. That means you can call any of the Graphics methods from the Pen class. In addition, the Pen class defines an API that ...

Get ActionScript 3.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.