Adobe, MAKE SOME NOISE

Hacked (yes really).

August 2nd, 2009

Hey readers - I want to apologize to anyone who tried to get to my last post and got a bunch of spam for cheap foreign pharmaceuticals instead. That certainly wasn't my intention (when I sell out, it won't be for gasex pills) and I'm taking steps to guard against this sort of thing in the future. If you want specifics on the hack, the best thread I've seen so far is here.

Sincerely,
-Rob

Modulo Grids

August 1st, 2009

Here's a quick optimization for people making grids of things. The traditional method is a pair of loops, nested to make rows and columns:

  1. const ROWS:int = 30;
  2. const COLUMNS:int = 88;
  3.  
  4. for(var row:int = 0; row < ROWS; row++){
  5. for(var column:int = 0; column < COLUMNS; column++){
  6. var s:Shape = new Shape();
  7. s.x = column * 5;
  8. s.y = row * 5;
  9.  
  10. s.graphics.beginFill(0x666666);
  11. s.graphics.drawRect(0, 0, 4, 4);
  12. addChild(s);
  13. }
  14. }

This can be simplified and sped up by taking advantage of the relationship between division and modulus to generate the row and column dynamically:

  1. const ROWS:int = 30;
  2. const COLUMNS:int = 88;
  3. const ITEMS:int = ROWS * COLUMNS;
  4.  
  5. for(var i:int = 0; i < ITEMS; i++){
  6. var s:Shape = new Shape();
  7. s.x = int(i / ROWS) * 5; // this is the same as Math.floor(i / ROWS), just faster
  8. s.y = (i % ROWS) * 5;
  9.  
  10. s.graphics.beginFill(0x666666);
  11. s.graphics.drawRect(0, 0, 4, 4);
  12. addChild(s);
  13. }

By upgrading to loop-invariants, we can cut the nested for-loop for a nice boost. And, as a bonus, this new code can work with sets that don't end squarely at the end of a column.