The drawPath method in Flash CS4

Flash CS4 has some new capabilities with basic applications in addition to the cool new 3D methods we have written about previously. Many of these capabilities can be achieved in Flash CS3 as well, but the new principles lead to more streamlined code that takes advantage of Flash Player 10. This brief tutorial will create an applet that draws a polygon with any number of points and fills it in when all points have been added. The following applet will be our final example in this short tutorial:

In doing this, we will illustrate the following new features of Flash CS4:

  • The use of the Vector class to create an "array" of objects of the same type.
  • The use of the drawPath method for display objects that makes creating a filled shape from data points quick and simple.
  • The fill options in the drawPath method allow for more flexibility in filling a shape created with the drawPath method.

We should note that Chapter 12 of the book, Flash and Math Applets: Learn by Example, includes an applet that uses the drawPath method to do a simple graph of the sine curve. While the book and this site contain several examples of the same basic idea in earlier versions of Flash, we mention the book applet since it illustrates how to start incorporating meaningful Flash CS4 commands into existing material on the flashandmath.com site.

Syntax of the drawPath method

The drawPath method is called with the following syntax:

drawPath(commands, data, winding)

See the Adobe Flash CS4 help file for this command for the full details of what will be illustrated in our examples.

Why use the drawPath method? According to the Adobe documentation,

  • drawings render faster with drawPath() than with a series of individual lineTo() and curveTo() methods;
  • the drawPath() method uses a uses a floating computation so rotation and scaling of shapes is more accurate and gives better results; and
  • the drawPath() method also uses slightly different rules for filling and drawing lines, as we shall see.

Download

Download all three fla files for this tutorial in the following compressed folder.

Back to Basic Tutorials              Back to Flash and Math Home

We welcome your comments, suggestions, and contributions. Click the Contact Us link below and email one of us.

Adobe®, Flash®, ActionScript®, Flex® are registered trademarks of Adobe Systems Incorporated.