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().
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.
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.
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.
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
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.