More drawing in Flash with ActionScript
More drawing in Flash with ActionScript
In the previous article Drawing in Flash with ActionScript, I introduced you to the drawing API in Flash. We looked at how you could create simple shapes like squares, circles and ellipses using ActionScript 1.0. We created an empty movie clip to hold the shapes and used code to change the line style and fills.
The drawing API is more useful if you create functions that you can reuse throughout your Flash movies. In this tutorial, I’ll show you how to create reusable functions to draw squares, rectangles, triangles, circles and ellipses. We’ll finish by creating new movie clip methods so you can draw shapes over and over again by including an external ActionScript file.
We’ll create functions to draw a:
- Square
- Rectangle
- Diamond
- Triangle
- Circle
- Ellipse
At the end of the tutorial, we’ll convert these to methods of the MovieClip in an ActionScript file.
As with the previous article, I’ve used ActionScript 1.0 for the examples in the tutorial so you’ll need at least Flash MX to complete the exercises here. Again, I’ve assumed that you’ve got some experience in working with ActionScript and that you know how to add code to the Actions panel.
You can download the finished files from the blue Properties box that contains the article PDF. There's a heading titled Code Download and you can click the Details link next to it to get the zip file containing all of the samples.
Note: If you have difficulties downloading the source files or PDF, you might have a problem with your cookies. Delete the cookies from your machine and try again. In Internet Explorer, you can do this by choosing Tools > Internet Options... and clicking the Delete Cookies... button on the General tab.
We’ll start by writing a function to create a perfect square.
Overview
Writing a draw square function
To create a square, we need to know the length of the sides and the position of the centre point for the shape. We’ll pass these values in as parameters to our function. We’ll also pass in the container movie clip instance name so we can draw the square in any existing movie clip.
Basically, each side is half the length away from the centre point of the square. We can start at one of the points and locate each of the other points by adding or subtracting half the length from the centre point. Don’t forget that the centre point is relative to the current movie clip NOT the Stage.
The diagram below shows you what I mean.
![](downloads/images/pict_1.jpg)
Because we’re working in a square where all sides are equal, the width and heights are the same.
Start by creating a new Flash movie. Change the name of Layer 1 to actions. Add the code below to your actions layer.
Hello. I'm the Principal of Anything Is Possible, an Australian web development business specialising in web applications development and training. I'm interested in using Flash with dynamic content and I've presented at a number of International conferences on topics relating to applications development, XML and scripting components. I have recently released my second print book Beginning XML with DOM and Ajax to match the first one - Foundation XML for Flash. I have a business web site
and a personal web site.
See All Postings From Sas Jacobs >>
Reviews
Be the first to write a review
You must me logged in to write a review.