Recipe 7.7. Drawing a Circle


Problem

You want to draw a circle at runtime.

Solution

Use the Grahics.drawCircle( ) method.

Discussion

Drawing a circle with the standard drawing methods is not as simple as you might think. Because of Flash's simplified, single control point Bezier calculations, it requires at least eight segments to create a circle that looks convincingly like a circle. Making the calculations in order to draw each segment of the circle requires a fair amount of math and code. However, the Graphics class has a drawCircle( ) method that greatly simplifies drawing a circle. The drawCircle( ) method requires three parameters:


x

The x coordinate of the circle's center point.


y

The y coordinate of the circle's center point.


radius

The radius of the circle.

The following draws a circle with a radius of 50 and the center point at 100,100:

sampleSprite.graphics.lineStyle(  ); sampleSprite.graphics.drawCircle(100, 100, 50);

Drawing concentric circles is simple enough; just specify the same center point for the circles:

sampleSprite.graphics.lineStyle(  ); sampleSprite.graphics.drawCircle(100, 100, 50); sampleSprite.graphics.drawCircle(100, 100, 100);

You can fill a circle by calling beginFill( ), beginGradientFill( ), or beginBitmapFill( ) before drawCircle( ), and calling endFill( ) after drawCircle( ):

sampleSprite.graphics.lineStyle(  ); sampleSprite.graphics.beginFill(0xFF0000); sampleSprite.graphics.drawCircle(100, 100, 50); sampleSprite.graphics.endFill(  );




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net