Category Archives: Object

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

Also posted in OOP, XML, dynamic | Tagged , | 3 Comments

Vector.sortOn()

Actionscript:
  1. var a:Vector.<Sprite> = new Vector.<Sprite>();
  2.  
  3. trace("unsorted");
  4. for (var i:int = 0; i<10; i++){
  5.     var s:Sprite = new Sprite();
  6.     s.x = int(Math.random()*100);
  7.     a.push(s);
  8.     trace(s.x);
  9. }
  10.  
  11. quickSortOn(a, "x", 0, a.length-1);
  12.  
  13. trace("sorted");
  14. for (i= 0; i<10; i++){
  15.     trace(a[i].x);
  16. }
  17.  
  18. // modified code from kirupa.com
  19. // http://www.kirupa.com/developer/actionscript/quickSort.htm
  20. function quickSortOn(a:Vector.<Sprite>, prop:String, left:int, right:int):void {
  21.     var i:int = 0, j:int = 0, pivot:Sprite, tmp:Sprite;
  22.     i=left;
  23.     j=right;
  24.     pivot = a[Math.round((left+right)*.5)];
  25.     while (i<=j) {
  26.         while (a[i][prop]<pivot[prop]) i++;
  27.         while (a[j][prop]>pivot[prop]) j--;
  28.         if (i<=j) {
  29.             tmp=a[i];
  30.             a[i]=a[j];
  31.             i++;
  32.             a[j]=tmp;
  33.             j--;
  34.         }
  35.     }
  36.     if (left<j)  quickSortOn(a, prop, left, j);
  37.     if (i<right) quickSortOn(a, prop, i, right);
  38. }
  39. /* outputs something like:
  40. unsorted
  41. 26
  42. 33
  43. 20
  44. 63
  45. 7
  46. 68
  47. 75
  48. 39
  49. 67
  50. 53
  51. sorted
  52. 7
  53. 20
  54. 26
  55. 33
  56. 39
  57. 53
  58. 63
  59. 67
  60. 68
  61. 75
  62. */

This demo is my first quick stab at using at a sortOn() function for the Vector class. It sorts a Vector of Sprites by their x property.

Recently there were a few times when I was prototyping ideas and suddenly realized that I needed to change my Vector to an Array because I needed to use sortOn().(If you don't already know, there is no built in sortOn() method for the Vector class). In the past I spent some time with sorting algorithms, bubble, insertion etc... so I knew I could pretty easily write my own sortOn(), but I also realized that a generic implementation wouldn't be easy/possible without loosing the type of the Vector. What I mean is, if you have a Vector of Sprites, you need a sorting method that takes a Vector.< Sprite > type as an argument (as seen above), if you have a Vector of TextFields you need a Vector.< TextField > type as an argument. You could of course use a generic type, but this kind of defeats the purpose of using a vector in the first place...

I will likely post a revised version of this in the near future with a slightly improved implementation of QuickSort. I haven't spent that much time with this, but If I recall correctly this is not the ideal implementation. I ported this code from a nice Kirupa tutorial and modified it to sort based on a property...

Also posted in Vector, arrays, associative arrays, sortOn | Tagged , | 3 Comments

var obj:Object = new Function()

Actionscript:
  1. var Pnt:Function = function(xp:Number, yp:Number){
  2.    
  3.     var x:Number = xp;
  4.     var y:Number = yp
  5.    
  6.     this.setX = function(v:int):void{
  7.         x = v;
  8.     }
  9.     this.setY = function(v:int):void{
  10.         y = v;
  11.     }
  12.     this.getX = function():int {
  13.         return x;
  14.     }
  15.     this.getY = function():int {
  16.         return y;
  17.     }
  18. }
  19.  
  20. var p:Object = new Pnt(10,10);
  21.  
  22. trace(p.getX(), p.getY());
  23. p.setX(100);
  24. trace(p.getX());
  25.  
  26. /*
  27. outputs:
  28. 10, 10
  29. 100
  30. */

Another way to define and instantiate Objects on the timeline. Interesting, but I don't recommend it over actual classes...

Also posted in OOP, functions | Tagged , | Leave a comment

Object.prototype Crap

Actionscript:
  1. Object.prototype.myVar = "I am a variable";
  2.  
  3. var s:Sprite = new Sprite();
  4.  
  5. trace(Object(s).myVar);
  6.  
  7. var v:Video = new Video();
  8. trace(Object(v).myVar);

This.... should not be done... and setting myVar like this:

Actionscript:
  1. Object(s).myVar = "hello";

Will cause an error....

I keep a folder on my laptop for this website..... when I have a random snippet idea I put it in this folder.... Every couple of weeks I go through this folder and turn select snippets into posts.... when I rediscovered this snippet today it really made me laugh....

Also posted in dynamic | Tagged , , | Leave a comment

Set Multiple Properties

Actionscript:
  1. // set multiple properties of an Object
  2. function setProps(o:*, props:Object):void{
  3.     for (var key:String in props){
  4.          o[key] = props[key];
  5.     }
  6. }
  7.  
  8. // example:
  9.  
  10. var s:Sprite = new Sprite();
  11. s.graphics.beginFill(0);
  12. s.graphics.drawRect(0,0,10,10);
  13. addChild(s);
  14.  
  15. // set some properties
  16. setProps(s, {x:100, y:100, scaleX:2, scaleY:2, rotation:45});

This was inspired by tweening engines like TweenLite.

Basically the same thing using a with statement:

Actionscript:
  1. with(s) x = 100, y = 100, scaleX = 2, scaleY = 2, rotation = 45;

Also posted in properties | Leave a comment

KeyCode Variables

Actionscript:
  1. // create constants for all letter and number keys:
  2. var alphabet:Array = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
  3. var nums:Array = ["ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"];
  4.  
  5. var key:Object = new Object();
  6. for (var i:int  = 0; i<alphabet.length; i++)
  7.      key[alphabet[i]] = 65 + i;
  8.      
  9. for (i = 0; i<nums.length; i++){
  10.      var code:int = 48 + i;
  11.      key[nums[i]]= code;
  12.      key[i] = code;
  13. }
  14.      
  15.  
  16. // test them out
  17. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  18. function onKeyPressed(evt:KeyboardEvent):void {
  19.     if (evt.keyCode == key.A){
  20.         trace("the a was pressed");
  21.     }
  22.     if (evt.keyCode == key.B){
  23.         trace("the b was pressed");
  24.     }
  25.     if (evt.keyCode == key.NINE){
  26.         trace("the 9 key was pressed");
  27.     }
  28.     if (evt.keyCode == key["0"]){
  29.         trace("the 0 key was pressed");
  30.     }
  31. }

This is an easy way to store values for alphanumeric keys in variables that make sense... instead of having to do things like this:

Actionscript:
  1. // hit the zero key
  2. if (evt.keyCode == 48){
  3.    trace("the zero key was hit");
  4. }

Also posted in keys | Tagged , , | Leave a comment