Category Archives: misc

The Big init() (nesting DisplayObjects)

Actionscript:
  1. function nest(top:DisplayObjectContainer, structure:Array):void{
  2.        var p:DisplayObjectContainer;
  3.        var key:String;
  4.        for (var i:int = 0; i<structure.length; i++){
  5.                if (structure[i] is DisplayObjectContainer){
  6.                     p = structure[i];
  7.                     if (!p.parent){
  8.                       top.addChild(p);
  9.                     }
  10.                     i++;
  11.                     if (structure[i] is Array){
  12.                                var child:DisplayObject;
  13.                                for (var j:int= 0; j<structure[i].length;  j++){
  14.                                        var val:* = structure[i][j];
  15.                                        if (val is DisplayObject){
  16.                                                child = val;
  17.                                                p.addChild(child);
  18.                                        }else{
  19.                                                for (key in val){
  20.                                                   child[key] = val[key];  
  21.                                                }
  22.                                        }
  23.                                }
  24.                        }else{
  25.                                trace("error, expecting Array");
  26.                                break;
  27.                        }
  28.                }else{
  29.                 trace("error, expecting DisplayObjectContainer (Sprite or MovieClip)");
  30.                        break;
  31.                }
  32.        }
  33. }
  34.  
  35. // create some filler graphics
  36. var window:Sprite = createWindow();
  37. window.x = 10;
  38. window.y = 10;
  39.  
  40. var c0:Sprite = createCircle();
  41. var c1:Sprite = createCircle();
  42. var c2:Sprite = createCircle();
  43. var c3:Sprite = createCircle();
  44. var c4:Sprite = createCircle(0xEFEFEF);
  45.  
  46. var box:Sprite = createBox(60, 50);
  47.  
  48.  
  49. var txt:TextField = new TextField();
  50. txt.text = "HI THERE";
  51.  
  52. // test out the function....
  53. nest(this, [window, [ c0, {x:100, y:100, scaleX:.5, scaleY:.5},
  54.                                     c1, {x:120, y:100, scaleX:.6, scaleY:.6},
  55.                                     c2, {x:140, y:100, scaleX:.7, scaleY:.7},
  56.                                     c3, {x:160, y:100, scaleX:.8, scaleY:.8},
  57.                                     box, {x:10, y:10} ],
  58.                         box, [ txt, c4,{x :30, y:30} ] ]);
  59.  
  60.  
  61. // just some quick functions to create filler graphics
  62.  
  63. function createBox(w:Number, h:Number):Sprite{
  64.     var s:Sprite = new Sprite();
  65.     with(s.graphics) beginFill(0x666666), drawRect(0,0, w, h);
  66.     return s;
  67. }
  68. function createWindow():Sprite{
  69.        var s:Sprite = new Sprite();
  70.        with(s.graphics) beginFill(0xCCCCCC), drawRect(0,0,300,300);
  71.        return s;
  72. }
  73. function createCircle(col:uint = 0xFF0000):Sprite{
  74.        var s:Sprite = new Sprite();
  75.        with(s.graphics) beginFill(col), drawCircle(0,0,10);
  76.        return s;
  77. }

The above uses a function called nest(). nest() is sort of like a superAddChild() function... it is meant to do the same thing as addChild, but with any number of parents and children.

I'm actually not very happy with this function, it was fun to write... but the 2D-Array/Object format is kind of odd looking (see line 53). So for at least the next day or two I'm going to try and think of the best format for a function that could do any number of addChild() calls with any number of children or parents. I realized that I also want to be able to set the properties of any of the children... so one solution might be an xml argument... something like:

Actionscript:
  1. <window>
  2.   <c0 x="100" y="100" />
  3.   <c1 x="200" y="100" />
  4.   <c2 x="300" y="100" />
  5.   <box x="400" y="100" alpha=".5">
  6.      <c3 x="10" y="10" />
  7.   </box>
  8. </window>

That's easy enough to do... it would be nice to think of something more minimalistic, but I think XML is much easier to read than JSON style Object syntax...

It's actually interesting to try and think of minimalistic ways to represent tree structures... check out this failed attempt:

Actionscript:
  1. p(window);
  2. __(c0, {x:100, y:100});
  3. __(c1, {x:100, y:100});
  4. __(c2, {x:100, y:100});
  5.  
  6. p(__(box, {x:100, y:100}));
  7. __(txt, c4, {x:30, y:30});

After writing the above... a few hours later I thought of this:

Actionscript:
  1. propOrder("x", "y", "scaleX", "scaleY", "alpha");
  2.  
  3. bigInit(this, [window, [info, 10, 10,
  4.                         about, 20, 10,
  5.                         contact, 30, 10,
  6.                         box, 40, 10, 2, 2, .5],
  7.                   box, [txt0, 10, 10 * off(),
  8.                         txt1, 10, 10 * off(),
  9.                         txt2, 10, 10 * off(),
  10.                         txt3, 10, 10 * off()],
  11.                   border]);

I think that's pretty close to what I was imagining in terms of simplicity.. the off() function would return an incremental offset... so call it once and it's 0, call it again and it's 1 etc... still not completely happy with this, but it's getting there...

Any additional thoughts on this topic will be in tomorrows post I think.

Posted in misc | Tagged , | Leave a comment

Visualizing Binary Numbers

Actionscript:
  1. [SWF(width=320,height=512,backgroundColor=0x000000,frameRate=30)]
  2.  
  3. var canvas:BitmapData=new BitmapData(32,512,false,0xFFFFFF);
  4. addChild(new Bitmap(canvas)).scaleX = 10;
  5.  
  6. var a:uint ;
  7. var s:String;
  8. var m:Number = 0;
  9. var d:Number = 0;
  10. var mi:int ;
  11. var r:Number = 0xFFFFFF / stage.stageWidth;
  12.  
  13. addEventListener(Event.ENTER_FRAME, onLoop);
  14.  
  15. function onLoop(evt:Event):void {
  16.    
  17.     d = mouseX * r;
  18.     m += (d - m) / 30;
  19.    
  20.     mi = int(m);
  21.  
  22.     canvas.lock();
  23.     canvas.fillRect(canvas.rect, 0xFFFFFF);
  24.  
  25.     a = 0xFFFFFFFF;
  26.     for (var i:int = 0; i<512; i++) {
  27.         s = (a -= mi).toString(2);
  28.         for (var j:int = 0; j<s.length; j++) {
  29.             if (s.charAt(j)=="0") {
  30.                 canvas.setPixel(j, i, 0x000000);
  31.             }
  32.         }
  33.     }
  34.     canvas.unlock();
  35. }

The above uses setPixel() to visualize numbers in binary format. You can move your mouse left and right to change an incremental counting value....

Here's a still generated by this snippet:

Also posted in BitmapData, pixel manipulation | Tagged , | 3 Comments

Snippet Template (imports)

Actionscript:
  1. package {
  2.     import adobe.utils.*;
  3.     import flash.accessibility.*;
  4.     import flash.display.*;
  5.     import flash.errors.*;
  6.     import flash.events.*;
  7.     import flash.external.*;
  8.     import flash.filters.*;
  9.     import flash.geom.*;
  10.     import flash.media.*;
  11.     import flash.net.*;
  12.     import flash.printing.*;
  13.     import flash.profiler.*;
  14.     import flash.sampler.*;
  15.     import flash.system.*;
  16.     import flash.text.*;
  17.     import flash.ui.*;
  18.     import flash.utils.*;
  19.     import flash.xml.*;
  20.    
  21.     dynamic public class Snippet extends MovieClip {
  22.         public function Snippet() {
  23.              // paste your snippet here (functions and all)
  24.         }
  25.     }
  26. }

This snippet imports all flash packages and is dynamic... you can copy actionsnippet code into the constructor of this file if you use Flex, FlashDevelop, TextMate etc... I tested it with a bunch of snippets and it seems to work nicely.

When I first teach classes in AS3 this is the template I use:

Actionscript:
  1. package{
  2.     import flash.display.*;
  3.     import flash.events.*;
  4.      
  5.     public class Main extends Sprite{
  6.         // etc...
  7.     }
  8. }

Display and events cover a lot of ground..... next one I find myself adding is flash.geom, followed by flash.net... I'd say those are my top 4 most frequently used packages.... I do lots of text layout in the Flash IDE, otherwise flash.text would be in there....

Also posted in OOP, dynamic, timeline | Tagged , | 4 Comments

NAND

Actionscript:
  1. var a:int, b:int;
  2.  
  3. a = 0;
  4. b = 0;
  5.  
  6. trace(int(!(a & b)));
  7.  
  8. a = 0;
  9. b = 1;
  10.  
  11. trace(int(!(a & b)));
  12.  
  13. a = 1;
  14. b = 0;
  15.  
  16. trace(int(!(a & b)));
  17.  
  18. a = 1;
  19. b = 1;
  20.  
  21. trace(int(!(a & b)));
  22.  
  23. /*
  24. outputs:
  25. 1
  26. 1
  27. 1
  28. 0
  29. */
  30.  
  31. /*
  32. NAND
  33. 00    1
  34. 01    1
  35. 10    1
  36. 11    0
  37. */

I started reading "The Elements of Computing Systems: Building a Modern Computer from First Principles" By Noam Nisan and Shimon Schocken. So far it's a very fun read. They talk about the power of NAND in the first chapter....

Also posted in Operators | Tagged , | Leave a comment