Category Archives: arrays

2D Z-Sort

Actionscript:
  1. var shapes:Array = new Array();
  2.  
  3. for (var i:int = 0; i<300; i++){
  4.     var s:Shape = new Shape();
  5.     s.x = Math.random()* stage.stageWidth;
  6.     s.y = Math.random()* stage.stageHeight;
  7.     s.graphics.lineStyle(0,0x000000);
  8.     s.graphics.beginFill(0xCCCCCC);
  9.     s.graphics.drawCircle(0,0, Math.random()*s.y / 5);
  10.     shapes.push(s);
  11. }
  12.  
  13. // comment this out to remove sorting
  14. shapes.sortOn("width", Array.NUMERIC);
  15.  
  16. for (i = 0; i<shapes.length; i++){
  17.     addChild(shapes[i]);
  18. }

This uses Array.sortOn() to do some basic z-sorting based on the width property of some circle shapes.

Also posted in sortOn | Tagged , | Leave a comment

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 Graphics | Leave a comment

Associative Array Function Call

Actionscript:
  1. for (var i:int = 0; i<4; i++){
  2.     this["phase"+i]();
  3. }
  4.  
  5. function phase0():void{
  6.     trace("phase 0");
  7. }
  8. function phase1():void{
  9.     trace("phase 1");
  10. }
  11. function phase2():void{
  12.     trace("phase 2");
  13. }
  14. function phase3():void{
  15.     trace("phase 3");
  16. }
  17.  
  18. /*
  19. will output:
  20. phase 0
  21. phase 1
  22. phase 2
  23. phase 3
  24. */
  25.  
  26. /*
  27. WARNING: This code was written for fun. Use at your own risk.
  28. */

I was pleasantly surprised back in the AS1 days... when I discovered that associative array syntax worked along with function calls (why wouldn't it?). There are some interesting tricks you can do with this technique.... will post some later....

Also posted in associative arrays, functions | Leave a comment

Quick Alphabet Array

Actionscript:
  1. var alphabet:Array = ("abcdefghijklmnopqrstuvwxyz").split("");
  2. trace("this is the letter b...", alphabet[1]);
  3. trace(alphabet);
  4. /* outputs:
  5. this is the letter b... b
  6. a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  7. */

This is an easy way to create an array of the alphabet. It's easier to type than:

Actionscript:
  1. var alphabet:Array = ["a","b","c".... etc];

Also posted in string manipulation, strings | 3 Comments