ActionScript 3 QuickTip #5: The Graphics API
In Flash 8 there were a bunch of properties and methods of the MovieClip class that allowed us to dynamically draw things at runtime. In ActionScript 3, we have these same capabilities along with some handy new ones. The drawCircle() and drawRoundRect() methods are two which will definitely save Flash devlopers quite a bit of time. The biggest difference in the AS 3 drawing API is that it is now its own class called Graphics and is attached to display objects such as MovieClip, Sprite, and Shape. You access the methods by writing something such as myClip.graphics.lineStyle(). The example below draws a line with a random color wherever your mouse moves. You can click the movie to clear the accumulated graphics. The AS 3 code is listed below that.
-
clip.addEventListener(MouseEvent.MOUSE_MOVE, drawLine);
-
clip.addEventListener(MouseEvent.CLICK, clearIt);
-
-
function drawLine(args:MouseEvent)
-
{
-
clip.graphics.lineStyle(15, Math.random()*0xBBD9F7);
-
clip.graphics.lineTo(args.localX, args.localY);
-
}
-
-
function clearIt(args:MouseEvent)
-
{
-
clip.graphics.clear();
-
}
Lee


Frog recently launched a public blog with a select group of prominent employees. Luckily I’ve convinced them to include me as well. This now makes a total of three blogs for me. My blog for frog, called Regular Expressions, will be more general in nature and won’t be tied to any particular technology. Make sure you come over and check it out. Please link to it a lot and post lots of comments so I can get a nice Christmas bonus 



