Tag Archives: flash

Loop Through All Properties of a Class

Actionscript:
  1. package {
  2.    
  3.     import flash.display.Sprite;
  4.     import flash.utils.describeType;
  5.    
  6.     public class Main extends Sprite {
  7.        
  8.         public function Main(){
  9.             var test:Test = new Test();
  10.             var desc:XML= describeType(test);
  11.             // public vars
  12.             for each (var v:XML in desc.variable){
  13.                 trace(v.@name, test[v.@name]);
  14.             }
  15.             // getters
  16.             for each (v in desc.accessor){
  17.                 trace(v.@name, test[v.@name]);
  18.             }
  19.         }
  20.        
  21.     }
  22. }
  23.  
  24. class Test{
  25.     public var a:Number = 123;
  26.     public var b:Number = 100;   
  27.     private var _getterVal:Boolean = false;
  28.     public function get getter():Boolean{
  29.         return _getterVal;
  30.     }
  31. }
  32. /*
  33. outputs:
  34. b 100
  35. a 123
  36. getter false
  37. */

I'm working on a few libraries, QuickBox2D and a library for auto-generated UI stuff... this technique just came in handy. It shows how to use describeType() to loop through public vars and getters of a given class.

The title of this post should really be Loop Through All PUBLIC properties of a class.... but it was long enough as it is....
Note: this should be run as document class

Posted in OOP, Object, XML, dynamic | Also tagged | 3 Comments

Point in Polygon

Actionscript:
  1. // function found here:
  2. // http://www.alienryderflex.com/polygon/
  3. function pointInPolygon(x:Number, y:Number, polyX:Array, polyY:Array):Boolean{
  4.     var j:int = polyX.length - 1;
  5.     var oddNodes:Boolean = false;
  6.     for (var i:int=0; i <polyX.length; i++) {
  7.     if (polyY[i] <y && polyY[j]>= y ||  polyY[j] <y && polyY[i]>= y) {
  8.  
  9.       if (polyX[i] + (y - polyY[i]) / (polyY[j] - polyY[i]) * (polyX[j] - polyX[i]) <x) {
  10.         oddNodes = !oddNodes;
  11.       }
  12.     }
  13.     j = i;
  14.   }
  15.   return  oddNodes;
  16. }
  17.  
  18. // draw an overly complex poly and store all  x y coords
  19. var pX:Array= new Array();
  20. var pY:Array = new Array();
  21. graphics.beginFill(0xCC0000);
  22. for (var i:int = 0; i<60; i++){
  23.     pX[i] = Math.random()*stage.stageWidth;
  24.     pY[i] =Math.random()*stage.stageHeight;
  25.     if (i == 0){
  26.         graphics.moveTo(pX[i], pY[i]);
  27.     }else{
  28.         graphics.lineTo(pX[i], pY[i]);
  29.     }
  30. }
  31. addEventListener(Event.ENTER_FRAME, onLoop);
  32. function onLoop(evt:Event):void {
  33.        alpha = 1;
  34.        if (pointInPolygon(mouseX, mouseY, pX, pY)){
  35.             alpha = .5;
  36.        }
  37. }

This snippet shows how to test to see if a point is inside a polygon. This may not really be useful as hitTestPoint() is likely faster - however this could be modified for other purposes such as polygon to polygon collision. This algorithm works nicely on all kinds of polygons ... convex, concave etc...

I found this algorithm in an article by Darel Rex Finley ... here.

I've used this function in Processing a few times...

Posted in graphics algorithms, misc | Also tagged | 2 Comments

Multiton

Actionscript:
  1. package {
  2.  
  3.     public class Multiton {
  4.        
  5.         public var test:String = "";
  6.        
  7.         private static  var _okToCreate:Boolean;
  8.         private static  var _instances:Object = new Object();
  9.         private static var _instanceNum:int = 0;
  10.  
  11.         public function Multiton() {
  12.             if (_okToCreate) {
  13.                 _instanceNum++;
  14.                 trace("created instance: " +_instanceNum);
  15.             } else {
  16.                 throw new Error(this + " is a Multiton... you must use the getInstance() method to instantiate it.");
  17.             }
  18.         }
  19.         public static function getInstance(key:String="default"):Multiton {
  20.             if (!_instances[key]) {
  21.                 _okToCreate = true;
  22.                 _instances[key] = new Multiton();
  23.                 _okToCreate = false;
  24.             }
  25.             return _instances[key];
  26.         }
  27.     }
  28. }

Holding off on the follow up to yesterdays post because I haven't finished it.... instead, here's a Multiton - Just like a Singleton but with multiple instances. You use it like this:

Actionscript:
  1. var m0:Multiton = Multiton.getInstance("one");
  2. m0.test = "i am instance one";
  3.  
  4. var m1:Multiton = Multiton.getInstance("two");
  5. m1.test = "i am instance two";
  6.  
  7. // elsewhere in the app
  8.  
  9. var multiton:Multiton = Multiton.getInstance("one");
  10. trace(multiton.test);
  11.  
  12. trace(Multiton.getInstance("two").test);

I've used this a few times and I seem to recall the Pure MVC makes use of this pattern (could be wrong about that though).

Posted in OOP | Also tagged | Leave a comment

Private Square Bracket Syntax

Actionscript:
  1. package {
  2.    
  3.     import flash.display.Sprite
  4.    
  5.     public class Test extends Sprite {
  6.        
  7.         private var _test:String = "private square brace syntax";
  8.         public function Test(){
  9.             trace(this["_test"]);
  10.         }
  11.     }
  12. }

It's important to note that square bracket syntax works with private vars... tomorrow I'll post a class I wrote that makes use of this technique to preserve encapsulation in an interesting way...

(auto-post didn't post this one yesterday for some reason... weird wordpress bug maybe?)

Posted in Uncategorized | Also tagged | 1 Comment