Multiple Graphics Class Calls

// draw red circle
with (graphics) beginFill(0xFF0000), drawCircle(200,100,30);

// draw 100 gray circles
with (graphics) for (var i:int = 0; i<100; i++) beginFill(0x666666), drawCircle(Math.random()*200, Math.random()*200, Math.random()*10), endFill();

// draw a few lines
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:

with(graphics) {
	beginFill(0xFF0000);
	drawRect(0,0,100,100);
	endFill();
	beginFill(0xFFFF00);
    drawRect(10,10,80,80);
	endFill();
	beginFill(0x0000FF);
	drawCircle(50,50,40);
}

2 comments

  1. 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(){}

Leave a Reply

Your email address will not be published. Required fields are marked *