How to Draw a Rectangle with ActionScript 3.0

I spent the last weekend working on a Flash animation for a client and I came across a few things that gave me a headache and other things that I thought I should share with the world that I thought were just intereting.
If you have ever messed around with ActionScript in Flash then you understand just how powerful scripting in Flash could be. I know they just released CS4 but the ActionScript version it uses is still 3.0, so whether you are using CS3 or CS4 this will still work for you.
Sure drawing a rectangle is no big feat but there are a few little things that make it a little bit complicated when trying to draw one entirely in ActionScript.

The following code is how you were draw a rectangle as a Shape object. It would also be the same if you were drawing it as a MovieClip.

var rectangle:Shape = new Shape; // initializing the variable named rectangle
rectangle.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red
rectangle.graphics.drawRect(0, 0, 100,100); // (x spacing, y spacing, width, height)
rectangle.graphics.endFill(); // not always needed but I like to put it in to end the fill
addChild(rectangle); // adds the rectangle to the stage

flash-rect

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie