Monthly Archives: February 2009

Is This swf Online?

Actionscript:
  1. private var _correctPath:String;
  2.  
  3. //.... somewhere a little later
  4.  
  5.  if (root.loaderInfo.url.split("http://").length == 1){
  6.         _correctPath = "http://www.mywebsite.com/this/is/an/absolute/path/";
  7.     }else{
  8.         _correctPath = "";
  9.     }

I like to do something like this when I'm working locally... it automatically tells my swf to use an absolute path for php files, xml files etc.... then, when I upload it, the LoaderInfo.url property contains an "http://" so it uses a relative path for all the external files. This is common especially if you don't have php and mysql installed on your machine and need to test these using your server.

Posted in misc | Tagged , | 4 Comments

The Big init() #2

Actionscript:
  1. // see previous post for details, this code won't run on it's own
  2. with(addChild(window)){
  3.     x = 10, y = 10;
  4.     with(addChild(c0)) x = 10, y = 10;
  5.     with(addChild(c1)) x = 20, y = 10;
  6.     with(addChild(box)){
  7.         x = 50, y = 10;
  8.         with(addChild(txt)) x = 5, y = 5;
  9.     }
  10. }

It's entertaining to try and think of different DisplayObject nesting techniques. I have a few more of these rolling around in my head that I'll try out soon.... The only good/notable thing about the above is that it shows that nested with statements actually work like you'd expect them to....

Posted in misc | Tagged , | Leave a comment

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

“in” Operator

Actionscript:
  1. var obj:Object = {name: "Joe", hobby: "Guitar", age:"80"};
  2.  
  3. if ("name" in obj){
  4.     trace(obj, " has a name property");
  5. }
  6.  
  7. if ("x" in obj == false){
  8.     trace(obj, " doesn't have an x property");
  9. }
  10.  
  11.  
  12. var mySprite:Sprite = new Sprite();
  13.  
  14. if ("currentFrame" in mySprite == false){
  15.     trace(mySprite, "does not have a currentFrame property");
  16. }

The "in" operator can be used to check if an object has a specific property. In the above example I test a standard Object instance and a Sprite instance for a few different properties.

I could see this operator being used when dealing with * (wildcard) datatypes and dynamic style code....

Posted in Operators, dynamic | Tagged , | Leave a comment