Quantunet.com

My Account

Joins Us
Flash 8 Knowledgebase

Creating Shape Drawing Fucntions

 

Drawing Shapes Using A Custom Fucntion

As we have seen in [Creating Custom Functions Statements] The general structure of a function statement can be defined like so:

function = functionName (variable1:Number, variable2:Number):Void{
do something;
}

And then called in the timeline like so:

functionName(3,8);

In the following articel we are going to explore how to create custom functions that can build shapes using built in flash drawing api methods.

Creating a Square

One of the simplest shapes to build is the square. Essentially all the user should need to put into the the function is the length of one side. Then idealy our function should be able to calcuate all the required coordinates and construct the shape using basic drawing methods such as MoveTo and lineTo().

For example: A Simple Square

 

this.createEmptyMovieClip("my_square_mc", 1);
my_square_mc._x = 50;
my_square_mc._y = 50;

drawSquare(my_square_mc, 125);

function drawSquare(target_mc:MovieClip, boxSize:Number):Void {

target_mc.lineStyle(3,0xFF0000,100);
target_mc.moveTo(0, 0);
target_mc.lineTo(boxSize, 0);
target_mc.lineTo(boxSize ,boxSize);
target_mc.lineTo(0, boxSize);
target_mc.lineTo(0, 0);

}

FAn empty movie clip is created and positioned on tage at the point (50,50). Then the function "drawSquare" is called. Remeber that in flash wne actionscript is compiled function definitions are complied first and then the primary timline actions sequence (so its ok to cell the function before it defined). The funnction is then defined using a the variables "target_mc" and boxSize" as arguments. Then

lineStyle()

sets the style of the line making the line 3 pixels thick and the color red.

moveTo(0,0)

Moves the pen to the point (0,0). Since the pen is already at the point (0,0) this is effedctively redundant however it can be usefull to keep in our function in case we want to shift the starting point of the pen inthe future.

target_mc.lineTo(boxSize, 0);
target_mc.lineTo(boxSize ,boxSize);
target_mc.lineTo(0, boxSize);
target_mc.lineTo(0, 0);

This sequence of lineTo commands then moves the pen in a square pattern using the value of "boxSize" (125 pixels) to calcuate the exact coordniates of each corner.

Creating A Simple Rectangle

Rectangle are geometrically similar to squares the main difference between the two is that a rectangle's width is not equal to its height. Knowling this we can make a small modification to our square function and give our function the ability of creating rectangles as well as squares.

For Example:

 

this.createEmptyMovieClip("my_rectangle_mc", 1);
my_rectangle_mc._x = 25;
my_rectangle_mc._y = 25;

drawRectangle(my_rectangle_mc, 100, 150);

function drawRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number):Void {

target_mc.lineStyle(4,0x999999,100);
target_mc.moveTo(0, 0);
target_mc.lineTo(boxWidth, 0);
target_mc.lineTo(boxWidth ,boxHeight);
target_mc.lineTo(0, boxHeight);
target_mc.lineTo(0, 0);

}

By adding another variable we can set width of the box independently form the height of the box. This gives the user the option of creating a rectangle when defining the input values to the function. In this example we are creating a rectangle that is 100pixels wide and 150pixels high and positioning it's top left corner at the point (25,25). The sequnce of lineTo() commands then uses the variables boxWdith and boxHeight to calcuate the exact cordinates of the box.

Of course this rectangle is a simple perimeter. To draw in a color fill we must use beginFill() method.

For example: A Simple Rectangle With A Fill Color

 

this.createEmptyMovieClip("my_rectangle_mc", 1);
my_rectangle_mc._x = 75;
my_rectangle_mc._y = 75;

drawRectangle(my_rectangle_mc, 100, 75);

function drawRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number):Void {

target_mc.beginFill(0x66CCFF, 100);
target_mc.lineStyle(4,0x999999,100);
target_mc.moveTo(0, 0);
target_mc.lineTo(boxWidth, 0);
target_mc.lineTo(boxWidth ,boxHeight);
target_mc.lineTo(0, boxHeight);
target_mc.lineTo(0, 0);
target_mc.endFill();
}

In this example beginFill() is inserted before lineStyle()

One of the most powerfull aspects of using functions to build shapes is the ability shape itself to be modified by changing the variables when the function is called. However so far we have created shapes with fixed line styles and color fills. To fix that we can add more arguments to the function allowing use to set the style of the line and fill when we call the function in the timeline.

For example: Adding Fill Color, Alpha, Line Color, Height And Alpha Variables

 

this.createEmptyMovieClip("my_rectangle_mc", 1);
my_rectangle_mc._x = 25;
my_rectangle_mc._y = 25;

drawRectangle(my_rectangle_mc, 200, 150, 4, 0x999999, 100, 0xFF99FF, 100);

function drawRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number, lineHeight:Number, lineColor:Number, lineAplha:Number, fillColor:Number, fillAlpha:Number):Void {

with (target_mc) {
beginFill(fillColor, fillAlpha);
lineStyle(lineHeight,lineColor,lineAplha);
moveTo(0, 0);
lineTo(boxWidth, 0);
lineTo(boxWidth ,boxHeight);
lineTo(0, boxHeight);
lineTo(0, 0);
endFill();
}

 

Note: Using with() helps make the code more readable.

[A Simple Triangle Function]

Creating A Rounded Rectangle

For example:


 

this.createEmptyMovieClip("my_rectangle_mc", 10);
my_rectangle_mc._x = 75;
my_rectangle_mc._y = 75;

drawRoundedRectangle(my_rectangle_mc, 240, 180, 20, 4, 0x999999, 100, 0xFF9900, 100);

function drawRoundedRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number, cornerRadius:Number, lineHeight:Number, lineColor:Number, lineAlpha:Number, fillColor:Number, fillAlpha:Number):Void{

with (target_mc) {
beginFill(fillColor, fillAlpha);
lineStyle(lineHeight, lineColor, lineAlpha);
moveTo(cornerRadius, 0);
lineTo(boxWidth - cornerRadius, 0);
curveTo(boxWidth, 0, boxWidth, cornerRadius);
lineTo(boxWidth, cornerRadius);
lineTo(boxWidth, boxHeight - cornerRadius);
curveTo(boxWidth, boxHeight, boxWidth - cornerRadius, boxHeight);
lineTo(boxWidth - cornerRadius, boxHeight);
lineTo(cornerRadius, boxHeight);
curveTo(0, boxHeight, 0, boxHeight - cornerRadius);
lineTo(0, boxHeight - cornerRadius);
lineTo(0, cornerRadius);
curveTo(0, 0, cornerRadius, 0);
lineTo(cornerRadius, 0);
endFill();
}
}

 

[A Simple Rounded Triangle]

Drawing A Circle Using A Function

The beginFill(

For example:

 

this.createEmptyMovieClip("my_circle_mc", 1);
my_circle_mc._x = 30;
my_circle_mc._y = 30;

drawCircle(my_circle_mc, 100, 4, 0x66CCFF, 100, 0xFFFF00, 100);

function drawCircle(target_mc:MovieClip, radius:Number, lineHeight:Number, lineColor:Number, lineAlpha:Number, fillColor:Number, fillAlpha:Number):Void {
var x:Number = radius;
var y:Number = radius;

with (target_mc) {
beginFill(fillColor, fillAlpha);
lineStyle(lineHeight, lineColor, lineAlpha);
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();
}
}

You can clearly see the radial gradient pattern int he top left corner of the rectangle. The value for x has been changes to move the center of the gradient toward the x axis and the gradient box has been squashed into an oval gradient pattern as the width "w" is greater thant the height "h" of the box.

Drawing A Ellipse Using A Function

The beginFill(

For example:

 

this.createEmptyMovieClip("my_oval_mc", 1);
my_oval_mc._x = 30;
my_oval_mc._y = 80;

drawOval(my_oval_mc, 100, 60, 3, 0x999999, 100, 0xCC99FF, 100);

function drawOval(target_mc:MovieClip, aradius:Number, bradius:Number, lineHeight:Number, lineColor:Number, lineAlpha:Number, fillColor:Number, fillAlpha:Number):Void {
var x:Number = aradius;
var y:Number = bradius;

with (target_mc) {
beginFill(fillColor, fillAlpha);
lineStyle(lineHeight, lineColor, lineAlpha);
moveTo(x+aradius, y);

curveTo(aradius+x Math.tan(Math.PI/8)*bradius+y,Math.sin(Math.PI/4)*aradius+x,Math.sin(Math.PI/4)*bradius+y);
curveTo(Math.tan(Math.PI/8)*aradius+x,bradius+y, x,bradius+y);

curveTo(-Math.tan(Math.PI/8)*aradius+x,bradius+y,-Math.sin(Math.PI/4)*aradius+x,Math.sin(Math.PI/4)*bradius+y);
curveTo(-aradius+x,Math.tan(Math.PI/8)*bradius+y,-aradius+x, y);

curveTo(-aradius+x,-Math.tan(Math.PI/8)*bradius+y,-Math.sin(Math.PI/4)*aradius+x,-Math.sin(Math.PI/4)*bradius+y);
curveTo(-Math.tan(Math.PI/8)*aradius+x,-bradius+y, x,-bradius+y);

curveTo(Math.tan(Math.PI/8)*aradius+x,-bradius+y,Math.sin(Math.PI/4)*aradius+x,-Math.sin(Math.PI/4)*bradius+y);
curveTo(aradius+x,-Math.tan(Math.PI/8)*bradius+y,aradius+x, y);
endFill();
}
}

You can clearly see the radial gradie


© 2008 Quantunet LLC All Rights Reserved | Intellectual Property | Terms of Use | Privacy
Home | About Quantunet | FAQ's | Contact Us