-
function create(obj:Class, props:Object):*{
-
var o:* = new obj();
-
for (var p:String in props){
-
o[p] = props[p];
-
}
-
return o;
-
}
-
-
// test out the function
-
-
var txt:TextField = create(TextField, {x:200, y:100, selectable:false, text:"hello there", textColor:0xFF0000, defaultTextFormat:new TextFormat("_sans", 20)});
-
addChild(txt);
-
-
var s:Sprite = Sprite(addChild(create(Sprite, {x:100, y:100, rotation:45, alpha:.5})));
-
-
with (s.graphics) beginFill(0xFF0000), drawRect(-20,-20,40,40);
-
-
var blur:BlurFilter = create(BlurFilter, {blurX:2, blurY:8, quality:1});
-
-
s.filters = [blur];
This snippet shows a function called create() that takes two arguments. The first argument is the name of a class to instantiate. The second is an Object with a list of properties to set on a newly created instance of the class (referenced in the first argument).
This could be particularly useful for TextFields which for some reason have no arguments in their constructor.
This will currently only work for classes that have either all optional constructor arguments or no constructor arguments.