Drawing shortcut for MovieClip.graphics

March 1, 2010

Thought I’d share this little shortcut that I hadn’t really come across before, which definitely helps to keep your code that little cleaner when working with MovieClip.graphics in Flash.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
this.grapics.lineStyle(1, 0x00000);
this.graphics.moveTo(10, 10);
this.graphics.lineTo(10, 20);
this.graphics.lineTo(20, 20);
this.graphics.lineTo(20, 10);
this.graphics.lineTo(10, 10);
                   
this.graphics.beginFill(0xFF0000);
this.graphics.drawCircle(40, 40, 20);
this.graphics.endFill();
                   
this.graphics.beginFill(0x00FF00);
this.graphics.drawRoundRect(40, 0, 40, 20, 2, 2);
this.graphics.endFill();

Can also be written:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
with (this.graphics) {
    lineStyle(1, 0x00000);
    moveTo(10, 10);
    lineTo(10, 20);
    lineTo(20, 20);
    lineTo(20, 10);
    lineTo(10, 10);
   
    beginFill(0xFF0000);
    drawCircle(40, 40, 20);
    endFill();
   
    beginFill(0x00FF00);
    drawRoundRect(40, 0, 40, 20, 2, 2);
    endFill();
}

Pretty simple, and surprised I hadn’t seen (or thought of) it before.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • email
  • Facebook
  • Pownce
  • Technorati
  • TwitThis

1 Response

  1. I should point out that this approach is not strongly typed, and was just a piece of code I came across in another developers code, and it stood out to me as being a unique approach.

    That said, you could also keep the code (just as) clean AND strongly typed by setting a local variable to the graphics of the DisplayObject:

    var g:Graphics = this.graphics

    g.linestyle(1, 0×000000);
    g.moveTo(10, 10);
    g.lineTo(10, 20);
    g.lineTo(20, 20);
    g.lineTo(20, 10);

    etc

Leave a Reply