Skip to content


[AS 3] Drawing a Rectangle

A simple class using the drawRect method to draw a rectangle. By default this will render a blue 10×10 (yeah, I know it is a square) and place it at position 0,0 of the stage.

package
{
	import flash.display.Sprite;

	public class DrawRectangle extends Sprite
	{
		private var xPos:Number;
		private var yPos:Number;
		private var rWidth:Number;
		private var rHeight:Number;
		private var color:uint;

		public function DrawRectangle(xPos:Number=0,yPos:Number=0,rWidth:Number=10,rHeight:Number=10,color:uint=0x336699)
		{
			this.graphics.beginFill(color);
			this.graphics.drawRect(xPos,yPos,rWidth,rHeight);
			this.graphics.endFill();
		}
	}
}

Posted in Actionscript 3, Drawing API.


4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Quest says

    Quick, concise, and exactly what I was looking for. Thanks.

  2. Hiep says

    very useful, thank you manewc.

  3. Dizzy says

    Hmm.. how do you use this to draw the rectangle…
    simply instantiating doesen’t seem to work in flash CS4, AS3

  4. Mike says

    Dizzy: Add your rectangle to the display list, with addChild or AddChildAt



Some HTML is OK

or, reply to this post via trackback.