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.

This entry was posted in misc 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 *

*
*