Stage Grid

Actionscript:
  1. // try changing the size of  the stage
  2. [SWF(width = 600, height = 400)]
  3.  
  4. // try changing the tile size (make sure swf width and height are evenly divisible by this number)
  5. var tileSize:int = 20;
  6.  
  7.  
  8. var cols:int = stage.stageWidth / tileSize;
  9. var rows:int = stage.stageHeight / tileSize;
  10.  
  11. var grid:Sprite = Sprite(addChild(new Sprite()));
  12. grid.graphics.lineStyle(0,0x000000);
  13.  
  14. var i:int = 0;
  15.  
  16. for (i = 1; i<cols; i++){
  17.     var posX:Number = i * tileSize
  18.     grid.graphics.moveTo(posX, 0);
  19.     grid.graphics.lineTo(posX, stage.stageHeight);
  20. }
  21. for (i = 1; i<rows; i++){
  22.     var posY:Number = i * tileSize
  23.     grid.graphics.moveTo(0, posY);
  24.     grid.graphics.lineTo(stage.stageWidth, posY);
  25. }

This is a quick way to fill the stage with a grid. I like my grids to have square tiles with sizes like 5x5, 10x10 20x20 etc.... So as long as you make sure that you're swf width and height are evenly divisible by your tileSize, this snippet will work nicely.

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

Post a Comment

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

*
*