20
Feb
08

Graphics Class clear() Method

Over the past two weeks I've taught two different lessons covering the basic ins and outs of the Graphics class (Drawing API). Something I noticed is that it's not always immediately apparent when you're starting out that the clear() method can be used to animate dynamically drawn vector graphics. That is, in both my class demos I introduced clear() by using it to erase a canvas when a button was clicked. When I described the idea of continuously clearing and redrawing graphics to create an animation there was some confusion, so I created a simple demo on the fly:

addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);

function onLoop(evt:Event):void {
	graphics.clear();
	graphics.lineStyle(1,0x000000);
	graphics.moveTo(100,100);
	graphics.lineTo(mouseX, mouseY);
}

Copy this into your timeline if you're not sure what it does.

I then showed examples from the web where people were using the Graphics class clear() method for more interesting things than just drawing a simple line, and I thought I'd create another example for future lessons:

This movie requires Flash Player 9. Please update your player.

Here's the code, which can be placed in a timeline frame script:

ActionScript 3.0:
  1. var points:Array = new Array();
  2. var pointNum:int = 100;
  3. var canvas:Sprite = new Sprite();
  4. addChild(canvas);
  5.  
  6. for (var i:int = 0; i<pointNum; i++) {
  7.     points.push(new Point(100,100+i));
  8. }
  9.  
  10. addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
  11.  
  12. function onLoop(evt:Event):void {
  13.     canvas.graphics.clear();
  14.     canvas.graphics.lineStyle(10,0);
  15.     points[0].x = mouseX;
  16.     points[0].y = mouseY;
  17.     canvas.graphics.moveTo(points[0].x, points[0].y);
  18.     for (var i:int = 1; i <pointNum; i++) {
  19.         points[i].x += (points[i-1].x - points[i].x) / 2;
  20.         points[i].y += ((points[i-1].y) + 1 - points[i].y) / 2;
  21.         canvas.graphics.lineTo(points[i].x, points[i].y);
  22.     }
  23. }

In this demo there is an array of 100 points. On enter frame, points[0] is set to the mouse location. A for loop is then used so that point[1] follows point[0], point[2] follows point[1], etc. All the y values are offset by one pixel to create the vertical scrolling effect.

Print This Post Print This Post


7 Responses to “Graphics Class clear() Method”


  1. 1 michel May 15th, 2008 at 9:59 pm

    Thanx

  2. 2 Graphic Design Jun 16th, 2008 at 11:25 pm

    This is great! thanks for the post as I love action script and never thought to use " clear " in this manor! GREAT POST!!

  3. 3 Larry Kolopajlo Sep 17th, 2008 at 7:16 am

    Nice graphics. I'm learning ActionScript 3 and this type of thing is fun and motivates me tl learn more.

  4. 4 al_wleed Sep 29th, 2008 at 11:26 pm

    thank you so much

  5. 5 manh Oct 10th, 2008 at 8:10 pm

    I don't know effect of this code

    points[i].x += (points[i-1].x - points[i].x) / 2;
    points[i].y += ((points[i-1].y) + 1 - points[i].y) / 2;
    

    can you explain for me!

    thank you very much.

  6. 6 Rich Oct 10th, 2008 at 8:58 pm

    @manh, the code you cited updates the current x and y position of each point in the array. In the case of x, it adds half the distance between the x values of the current and previous points in the array. In the case of y, it does the same thing but also adds 1 to the distance before dividing. The equations can be read like this:

    new x of current point in loop equals (x of previous point in loop minus x of current point in loop) divided by two

    new y of current point in loop equals (y of previous point in loop plus 1 minus y of current point in loop) divided by two

  7. 7 Justin Dec 5th, 2008 at 12:38 pm

    That is a great little action script! Thanks for sharing!

Leave a Reply




Speaking

Archives