Drawing with ActionScript 545
To create a circle:
1. Create a new Flash document and save as circle2.fla.
2. Add the following ActionScript code to Frame 1 of the Timeline:
this.createEmptyMovieClip("circle_mc", 10);
circle_mc._x = 100;
circle_mc._y = 100;
drawCircle(circle_mc, 100, 0x99FF00, 100);
function drawCircle(target_mc:MovieClip, radius:Number,
fillColor:Number, fillAlpha:Number):Void {
var x:Number = radius;
var y:Number = radius;
with (target_mc) {
beginFill(fillColor, fillAlpha);
moveTo(x + radius, y);
curveTo(radius + x, Math.tan(Math.PI / 8) * radius + y,
Math.sin(Math.PI / 4) * radius + x, Math.sin(Math.PI / 4) * radius +
y);
curveTo(Math.tan(Math.PI / 8) * radius + x, radius + y, x, radius +
y);
curveTo(-Math.tan(Math.PI / 8) * radius + x, radius+ y, -
Math.sin(Math.PI / 4) * radius + x, Math.sin(Math.PI / 4) * radius +
y);
curveTo(-radius + x, Math.tan(Math.PI / 8) * radius + y, -radius +
x, y);
curveTo(-radius + x, -Math.tan(Math.PI / 8) * radius + y, -
Math.sin(Math.PI / 4) * radius + x, -Math.sin(Math.PI / 4) * radius +
y);
curveTo(-Math.tan(Math.PI / 8) * radius + x, -radius + y, x, -radius
+ y);
curveTo(Math.tan(Math.PI / 8) * radius + x, -radius + y,
Math.sin(Math.PI / 4) * radius + x, -Math.sin(Math.PI / 4) * radius +
y);
curveTo(radius + x, -Math.tan(Math.PI / 8) * radius + y, radius + x,
y);
endFill();
}
}
3.
Save your Flash document and select Control > Test Movie to test the SWF file.
This code creates a more complex, and realistic, circle than the previous circle example.
Instead of only using four calls to the
curveTo() method, this example uses eight calls to the
curveTo() method, which gives the shape a much rounder appearance.
You can use the Drawing API to create a triangle, as the following procedure shows.