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

Drawing Lines

To draw straight lines we use the lineTo( ) method, which draws a line from the current drawing pen position to a specified point (x, y). For example, the following code creates a new Sprite object and draws a line in it from (0, 0) to (25, 35):

var canvas:Shape = new Shape();
canvas.graphics.lineTo(25, 35);
addChild(canvas);

However, if you try that code as is, you may be surprised to find that no line appears on screen! By default, all lines and shapes drawn have no stroke. To cause a stroke to appear, we must use the lineStyle( ) method, which sets the visual stroke characteristics (thickness, color, etc.) for all lines and shapes subsequently drawn. For reference, here is the method signature for lineStyle( ), showing the visual options available and their default values. Consult Adobe's ActionScript Language Reference for details on each parameter.

lineStyle(thickness:Number = NaN,
          color:uint = 0,
          alpha:Number = 1.0,
          pixelHinting:Boolean = false,
          scaleMode:String = "normal",
          caps:String = null,
          joints:String = null,
          miterLimit:Number = 3)

The lineStyle( ) method must be invoked explicitly for each new Sprite, MovieClip, and Shape object, or no stroke will appear (although filled regions can still be drawn without a stroke).

Let's look at a few examples showing various ways to modify the line style of canvas. The following code clears the line style (subsequent lines, curves, and fills are not stroked):

canvas.graphics.lineStyle()

The following code sets the line style ...

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