Recipe 7.1.
Setting a Line Style
Problem
You want to set the line style properties.
Solution
Use the lineStyle( ) method.
Discussion
Before you can draw programmatically, you must
set the line style properties for a Graphics object. If you don't set
the line style properties, the default line style is undefined and
lines and fills won't render. You can set the line style properties
by using the lineStyle( ) method of a Graphics object.
The lineStyle( ) method accepts up to
eight parameters, all of which are optional. The parameters are as
follows:
thickness
-
The thickness (in pixels) for a line. The
default value is 1. The valid values range from 0 to 255. 0 is a
hairline. Values outside of the valid range are adjusted to the
nearest valid value.
color
-
The color for the line. The default value is
0x000000.
alpha
-
The alpha value for the line. The valid range is
from 0 to 1. The default value is 1.
pixelHinting
-
A Boolean value indicating whether or not the
lines ought to snap to whole pixels. The default is
false.
scaleMode
-
One of the constants from the flash.display.LineScaleMode
class. The constants are NORMAL (default), NONE,
VERTICAL, and HORIZONTAL. When the value is set
to NORMAL, the line thickness scales when the object
within which the line is drawn is scaled. For example, if a sprite
containing a 1-pixel line is scaled 200 percent, the line thickness
scales to 2 pixels. If you set the scale mode to NONE,
then the line thickness never scales. If you set the scale mode to
VERTICAL, then the line thickness does not scale when the
object is scaled only in the vertical direction (though it scales
when the object is scaled horizontally). Likewise, if you set the
scale mode to HORIZONTAL, the line thickness doesn't scale
when the object is scaled only in the horizontal direction.
caps
-
A string specifying the end caps to use for the
line. The value can be one of the flash.display.CapsStyle
constants. The constants are NONE, ROUND
(default), and SQUARE.
joints
-
A string specifying the type of joints between
connecting line segments. The value can be one of the flash.display.JointStyle
constants. The constants are BEVEL, MITER, and
ROUND (default).
miterLimit
-
When the joint style is set to MITER
you can optionally specify a miter
limit. The default value is 3. The valid range is from 1 to
255. The miter limit determines how far beyond the point at which
the line segments join that the miter joint extends. The effect of
a miter limit is determined by the value as well as the line
thickness and the angle between the line segments.
Since all the parameters are optional, you can
minimally call the lineStyle( ) method, as in the following
example:
sampleSprite.graphics.lineStyle( );
You can call the lineStyle( ) method at
any point to change the line style. For example, you can set the
line style to a 20 pixel green line, draw a line segment, set the
line style to a 10 pixel blue line, and then draw a second line
segment with the new line style.
You must set the line style again after calling
the clear( ) method.
Calling the clear( ) method effectively resets the line
style to the default undefined state.
|