Drawing a Circle using ActionScript

by Elis Frugalo.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on flash  

You are here: Categories » Computers and technology » Flash

Create a custom MovieClip.drawCircle( ) method using the Drawing API and invoke it on a movie clip.

You can create a circle in ActionScript with eight curves. Fewer curves results in a distorted circle and too many curves hinders performance. Let's create a custom method of the MovieClip class for drawing circles. This method, drawCircle( ), allows for three parameters:

radius
The radius of the circle

x
The x coordinate of the circle's center point. If undefined, the circle is centered at x = 0.

y
The y coordinate of the circle's center point. If undefined, the circle is centered at y = 0.

Define the custom drawCircle( ) method on MovieClip.prototype to make it available to all movie clip instances:

MovieClip.prototype.drawCircle = function (radius, x, y) {
   // The angle of each of the eight segments is 45 degrees (360 divided by 8), which
   // equals p/4 radians.
   var angleDelta = Math.PI / 4;
 // Find the distance from the circle's center to the control points for the curves.
   var ctrlDist = radius/Math.cos(angleDelta/2);
 // Initialize the angle to 0 and define local variables that are used for the 
   // control and ending points. 
   var angle = 0;
   var rx, ry, ax, ay;
 // Move to the starting point, one radius to the right of the circle's center.
   this.moveTo(x + radius, y);
 // Repeat eight times to create eight segments.
   for (var i = 0; i < 8; i++) {
 // Increment the angle by angleDelta (p/4) to create the whole circle (2p).
   angle += angleDelta;
 // The control points are derived using sine and cosine.
   rx = x + Math.cos(angle-(angleDelta/2))*(ctrlDist);
   ry = y + Math.sin(angle-(angleDelta/2))*(ctrlDist);
 // The anchor points (end points of the curve) can be found similarly to the 
   // control points.
   ax = x + Math.cos(angle)*radius;
   ay = y + Math.sin(angle)*radius;
 // Draw the segment.
   this.curveTo(rx, ry, ax, ay);
   }
   }

How the drawCircle( ) method functions is better understood with a little explanation.

The distance of the control point for each segment from the circle's center is found using a trigonometric formula that states that the cosine of an angle is equal to the adjacent side over the hypotenuse. In the case of the circle, the angle that bisects a segment (thus also intersecting its control point) is p/8 (angleDelta/2). The distance to the control point from the center of the circle forms the hypotenuse of the right triangle.

var ctrlDist = radius/Math.cos(angleDelta/2);

Basic trigonometric formulas can be used to find the x and y coordinates along the circle's circumference given the angle and the hypotenuse. For the control point, the hypotenuse value is ctrlDist, and the angle is angle - angleDelta/2, since this angle bisects the segment. The anchor point is found using the value of angle, which is calculated to be the angle that intersects the anchor point, and the circle's radius (since the anchor point should always be on the circle's circumference). Thus, it follows:

rx = x + Math.cos(angle-(angleDelta/2))*(ctrlDist);
   ry = y + Math.sin(angle-(angleDelta/2))*(ctrlDist);
   ax = x + Math.cos(angle)*radius;
   ay = y + Math.sin(angle)*radius;

Once you have defined the drawCircle( ) method and included it in your Flash document, you can quickly draw a circle with just a few lines of code. Remember that you still need to define a line style before Flash will draw anything.

// Create a movie clip instance in which you will draw the circle.
   this.createEmptyMovieClip("circle_mc", 1);
// Define a 1-pixel, black, solid line style.
   circle_mc.lineStyle(1, 0x000000, 100);
// Draw a circle of radius 100, centered at (50,75).
   circle_mc.drawCircle(100, 50, 75);
   // Draw a circle of radius 65, centered at (0,0).
   circle_mc.drawCircle(65);

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

this.createEmptyMovieClip("circle_mc", 1);
   circle_mc.lineStyle(1, 0x000000, 100);   // Use a 1-pixel, black, solid border.
   circle_mc.beginFill(0x0000FF);           // Use a solid blue fill.
   circle_mc.drawCircle(100);
   circle_mc.endFill(  );
 
Leave a comment or ask a question
Total comments: 0

Flash Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Transforming the Current Color of a Flash Movie Clip - You want to modify a movie clip's color relative to the current color transformation, instead of relative to the author-time color values. Use the getTransform( ) and setTransform( ) method (more...)
ActionScript: Repeating an Operation Many Times - You want to perform a task multiple times within a single frame. Use a loop to perform the same task multiple times within a single frame. For example, you can use a for statement: (more...)
Avoiding Conflicting Variables in ActionScript - You want to make sure that variables within a function do not interfere with variables in other functions or within the timeline in which the function is defined. Use the var keyword to declare loc (more...)
How to Exit a Function in ActionScript - You want to exit a function. Functions terminate automatically after the last statement within the function executes. Use a return statement to exit a function before reaching its end. The (more...)
Move Clip Rotation - A property like _x and _y is the movie clip property _rotation. The _rotation property accepts a value in degrees. A circle is divided into 360 degrees. The values used by _rotation range f (more...)
Targeting Flash Movie Clips - The simplest way to target a movie clip is to use its name, followed by a dot, followed by the command you want to send. However, there are plenty of other ways to target a movie clip as we (more...)
Using Mathematical Operators in ActionScript - You want to modify something over time, such as the rotation or position of a movie clip. Use the compound assignment operators to change a variable or property in increments. Or, if increm (more...)
How to build an animated button using Flash - Time: 15 Minutes. Difficulty Level: Intermediate Requirements: Flash 8 Assumed Knowledge: Basic action (more...)
Enhancing Standalone Projector - You want to create an enhanced Standalone Projector with features such as borderless playback, custom titles, no Flash menus, and so on. Use a third-party tool such as SWF Studio or SWFKit to c (more...)
Creating Flash Buttons - Buttons are one of the three main types of symbols that you use in Flash. The others are movie clips and graphics. Making a New Button There are many ways to create a button. One (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.