Multiple Graphics Class Calls

  1. // draw red circle
  2. with (graphics) beginFill(0xFF0000), drawCircle(200,100,30);
  4. // draw 100 gray circles
  5. with (graphics) for (var i:int = 0; i<100; i++) beginFill(0x666666), drawCircle(Math.random()*200, Math.random()*200, Math.random()*10), endFill();
  7. // draw a few lines
  8. with (graphics) lineStyle(0, 0x000000), moveTo(10,210), lineTo(20,300), lineTo(30,210), lineTo(40,300), lineTo(50,210), lineTo(60,300);

Sometimes it's tedious to write lines and lines of Graphics class method calls. Using a with statement you can reduce these calls into one long (not very readable) line of code.

More readable example:

  1. with(graphics) {
  2.     beginFill(0xFF0000);
  3.     drawRect(0,0,100,100);
  4.     endFill();
  5.     beginFill(0xFFFF00);
  6.     drawRect(10,10,80,80);
  7.     endFill();
  8.     beginFill(0x0000FF);
  9.     drawCircle(50,50,40);
  10. }

This entry was posted in Graphics. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.


  1. Posted November 6, 2008 at 12:31 am | Permalink

    Yes, I also use with(graphics) a lot, { } blocking makes the code more readable.

    What’s a bit pity that - at least Flash CS3 API - doesn’t show the code hints inside with(){}

  2. Posted November 7, 2008 at 8:15 am | Permalink

    yeah, that would be nice. One reason is because the CS3/CS4 editors use the dot operator to show the code hinting…

Post a Comment

Your email is never published nor shared. Required fields are marked *