Actionscript:
-
package {
-
-
import flash.display.Sprite;
-
import flash.utils.describeType;
-
-
public class Main extends Sprite {
-
-
public function Main(){
-
var test:Test = new Test();
-
var desc:XML= describeType(test);
-
// public vars
-
for each (var v:XML in desc.variable){
-
trace(v.@name, test[v.@name]);
-
}
-
// getters
-
for each (v in desc.accessor){
-
trace(v.@name, test[v.@name]);
-
}
-
}
-
-
}
-
}
-
-
class Test{
-
public var a:Number = 123;
-
public var b:Number = 100;
-
private var _getterVal:Boolean = false;
-
public function get getter():Boolean{
-
return _getterVal;
-
}
-
}
-
/*
-
outputs:
-
b 100
-
a 123
-
getter false
-
*/
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 actionscript |
Actionscript:
-
// function found here:
-
// http://www.alienryderflex.com/polygon/
-
function pointInPolygon(x:Number, y:Number, polyX:Array, polyY:Array):Boolean{
-
var j:int = polyX.length - 1;
-
var oddNodes:Boolean = false;
-
for (var i:int=0; i <polyX.length; i++) {
-
if (polyY[i] <y && polyY[j]>= y || polyY[j] <y && polyY[i]>= y) {
-
-
if (polyX[i] + (y - polyY[i]) / (polyY[j] - polyY[i]) * (polyX[j] - polyX[i]) <x) {
-
oddNodes = !oddNodes;
-
}
-
}
-
j = i;
-
}
-
return oddNodes;
-
}
-
-
// draw an overly complex poly and store all x y coords
-
var pX:Array= new Array();
-
var pY:Array = new Array();
-
graphics.beginFill(0xCC0000);
-
for (var i:int = 0; i<60; i++){
-
pX[i] = Math.random()*stage.stageWidth;
-
pY[i] =Math.random()*stage.stageHeight;
-
if (i == 0){
-
graphics.moveTo(pX[i], pY[i]);
-
}else{
-
graphics.lineTo(pX[i], pY[i]);
-
}
-
}
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
alpha = 1;
-
if (pointInPolygon(mouseX, mouseY, pX, pY)){
-
alpha = .5;
-
}
-
}
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...
Actionscript:
-
package {
-
-
public class Multiton {
-
-
public var test:String = "";
-
-
private static var _okToCreate:Boolean;
-
private static var _instances:Object = new Object();
-
private static var _instanceNum:int = 0;
-
-
public function Multiton() {
-
if (_okToCreate) {
-
_instanceNum++;
-
trace("created instance: " +_instanceNum);
-
} else {
-
throw new Error(this + " is a Multiton... you must use the getInstance() method to instantiate it.");
-
}
-
}
-
public static function getInstance(key:String="default"):Multiton {
-
if (!_instances[key]) {
-
_okToCreate = true;
-
_instances[key] = new Multiton();
-
_okToCreate = false;
-
}
-
return _instances[key];
-
}
-
}
-
}
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:
-
var m0:Multiton = Multiton.getInstance("one");
-
m0.test = "i am instance one";
-
-
var m1:Multiton = Multiton.getInstance("two");
-
m1.test = "i am instance two";
-
-
// elsewhere in the app
-
-
var multiton:Multiton = Multiton.getInstance("one");
-
trace(multiton.test);
-
-
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 actionscript |
Actionscript:
-
package {
-
-
import flash.display.Sprite
-
-
public class Test extends Sprite {
-
-
private var _test:String = "private square brace syntax";
-
public function Test(){
-
trace(this["_test"]);
-
}
-
}
-
}
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 actionscript |