drawRoundRect in AS2

Leave a comment 17 March 2010

Today I have been doing some retro AS2 coding (don’t ask why…) and needed to programmatically draw a rounded rectangle (or squirkle as my friend Si likes to call them). In AS3 this is a breeze as you have the drawRoundRect method of the Graphics Class. AS2 doesn’t have that, so I wrote* one:

class com.flashmonkey.display.RoundRect
  static public function draw(mc:MovieClip, col:Number, alpha:Number, x:Number, y:Number, w:Number, h:Number, radius:Number)
    col = (col == undefined) ? 0xFF00FF : col;
    alpha = (alpha == undefined) ? 100 : alpha;
    x = (x == undefined) ? 0 : x;
    y = (y == undefined) ? 0 : y;
    w = (w == undefined) ? 100 : w;
    h = (h == undefined) ? 100 : h;
    radius = (radius == undefined) ? 10 : radius;
    var circ:Number = 0.707107;
    var off:Number = 0.6;
    mc.beginFill(col, alpha);
    mc.moveTo(x+0, y+radius);
    mc.lineTo(x+0, y+h-radius);
    mc.curveTo(x+0, y+(h-radius)+radius*(1-off), x+0+(1-circ)*radius, y+h-(1-circ)*radius);
    mc.curveTo(x+(0+radius)-radius*(1-off), y+h, x+radius, y+h);
    mc.lineTo(x+w-radius, y+h);
    mc.curveTo(x+(w-radius)+radius*(1-off), y+h,x+w-(1-circ)*radius, y+h-(1-circ)*radius);
    mc.curveTo(x+w,y+(h-radius)+radius*(1-off), x+w, y+h-radius);
    mc.lineTo(x+w, y+0+radius);
    mc.curveTo(x+w, y+radius-radius*(1-off), x+w-(1-circ)*radius, y+0+(1-circ)*radius);
    mc.curveTo(x+(w-radius)+radius*(1-off), y+0, x+w-radius, y+0);
    mc.lineTo(x+radius, y+0);
    mc.curveTo(x+radius-radius*(1-off), y+0,x+(1-circ)*radius, y+(1-circ)*radius);
    mc.curveTo(x+0, y+radius-radius*(1-off), x+0, y+radius);

So you can use it (if your extremely unfortunate and have to code in AS2) like this:

import com.flashmonkey.display.RoundRect;


// or like this

RoundRect.draw(_root.createEmptyMovieClip("mc", _root.getNextHighestDepth()), 0x00FFFF, 50, 75, 75, 175, 100, 6);

* I actually didn’t write all the lineTo curveTo stuff, I stole it from somewhere a few years ago but can’t remember where from to credit the original developer – if it was you, email me and I will credit you! ;)

Posted in ActionScript

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>