Category Archives: Graphics

Multiple Graphics Class Calls

Actionscript:
  1. // draw red circle
  2. with (graphics) beginFill(0xFF0000), drawCircle(200,100,30);
  3.  
  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();
  6.  
  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:

Actionscript:
  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. }

Posted in Graphics | 2 Comments

2D Array Map

Actionscript:
  1. var col:Array = [0x000000, 0xCCCCCC, 0xFF0000, 0xCCCC00, 0x000055, 0x00CCCC];
  2.  
  3. var map:Array = new Array();
  4. map[0] = [0,0,0,0,0,0,0,0,0,0];
  5. map[1] = [0,0,0,0,0,0,0,0,0,0];
  6. map[2] = [0,5,4,5,4,5,4,5,4,0];
  7. map[3] = [0,4,4,4,4,4,4,4,4,0];
  8. map[4] = [0,4,0,0,0,0,0,0,4,0];
  9. map[5] = [0,3,2,3,2,3,2,3,2,0];
  10. map[6] = [1,1,1,1,1,1,1,1,1,1];
  11. map[7] = [0,2,2,2,2,2,2,2,2,0];
  12. map[8] = [1,1,1,1,1,1,1,1,1,1];
  13.  
  14. for (var i:int = 0; i<map.length; i++){
  15.     for (var j:int = 0; j<map[i].length; j++){
  16.         graphics.beginFill(col[map[i][j]]);
  17.         graphics.drawRect(j * 10, i * 10, 10, 10);
  18.     }
  19. }

Use a 2D array to draw a map of colored rectangles. Lots of stuff you can do with this - like adding game tiles instead of drawing colored rects:

Actionscript:
  1. // add clip with multiple frames - each containing a tile graphic
  2. var tile:MovieClip = TileClip();
  3. tile.gotoAndStop(map[i][j]+1);
  4. addChild(tile);

Also posted in arrays | Leave a comment

Rect vs Ellipse

Actionscript:
  1. for (var i:int = 0; i<100; i++){
  2.     graphics.lineStyle(0,0);
  3.     graphics[["drawEllipse", "drawRect"][int(Math.random()*2)]](Math.random()*400, Math.random()*300, Math.random()*100,Math.random()*100)
  4. }
  5.  
  6. /*
  7. WARNING: This code was written for fun. Use at your own risk.
  8. */

And a more readable version:

Actionscript:
  1. var methodChoices:Array = ["drawEllipse", "drawRect"];
  2. var method:String;
  3. var xp:Number, yp:Number, w:Number, h:Number;
  4. for (var i:int = 0; i<100; i++){
  5.     method = methodChoices[int(Math.random()*methodChoices.length)];
  6.     graphics.lineStyle(0,0);
  7.     xp = Math.random()*400;
  8.     yp = Math.random()*300;
  9.     w = Math.random()*100;
  10.     h = Math.random()*100;
  11.     graphics[method](xp, yp, w, h)
  12. }
  13.  
  14. /*
  15. WARNING: This code was written for fun. Use at your own risk.
  16. */

Here I use yesterdays associative array function call technique to do something different. Not really all that useful, but interesting....

Also posted in associative arrays, functions, random | Leave a comment