Category Archives: properties

Instantiate and Set Properties

Actionscript:
  1. function create(obj:Class, props:Object):*{
  2.     var o:* = new obj();
  3.     for (var p:String in props){
  4.         o[p] = props[p];
  5.     }
  6.     return o;
  7. }
  8.  
  9. // test out the function
  10.  
  11. var txt:TextField = create(TextField, {x:200, y:100, selectable:false, text:"hello there", textColor:0xFF0000, defaultTextFormat:new TextFormat("_sans", 20)});
  12. addChild(txt);
  13.  
  14. var s:Sprite = Sprite(addChild(create(Sprite, {x:100, y:100, rotation:45, alpha:.5})));
  15.  
  16. with (s.graphics) beginFill(0xFF0000), drawRect(-20,-20,40,40);
  17.  
  18. var blur:BlurFilter = create(BlurFilter, {blurX:2, blurY:8, quality:1});
  19.  
  20. 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.

Also posted in dynamic, functions, one-liners | Tagged , | Comments closed

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 Object | Leave a comment

Mini Tween Engine

Actionscript:
  1. var box:MovieClip = new MovieClip();
  2. box.graphics.beginFill(0xFF0000);
  3. box.graphics.drawRect(-25,-25,50,50);
  4. addChild(box);
  5.  
  6. stage.addEventListener(MouseEvent.CLICK, onStageDown);
  7. function onStageDown(evt:MouseEvent):void{
  8.     moveTo(box, "x", 300, 2, Back.easeOut);
  9.     moveTo(box, "y", 200, 2, Back.easeOut);
  10.     moveTo(box, "scaleX", 2, 2, Back.easeOut);
  11.     moveTo(box, "rotation", 180, 2, Quartic.easeOut, onDone);
  12.     stage.removeEventListener(MouseEvent.CLICK, onStageDown);
  13. }
  14.                      
  15. function onDone():void {
  16.     moveTo(box, "x", 120, 2, Back.easeIn);
  17. }
  18.  
  19. //
  20. // -- TWEEN ENGINE
  21. //
  22. import fl.motion.easing.*;
  23.  
  24. // movieClip to tween, property to tween, final value for the property, duration of tween,
  25. // ease type, complete callback function
  26. function moveTo(mc:MovieClip, prop:String, dest:Number, duration:Number, ease=null,
  27.                            completeFunction:Function=null):void {
  28.     if (ease == null) {
  29.         ease = Quartic.easeOut;
  30.     }
  31.     // use the property to make all var names unique
  32.     mc["begin" + prop] = mc[prop];
  33.     mc["change" + prop] =  dest - mc[prop];
  34.     mc["time" + prop] = 0;
  35.     mc["startTime" + prop] = getTimer();
  36.     mc["duration" + prop] = duration;
  37.     mc["prop"] = prop;
  38.     mc["ease" + prop] = ease;
  39.     mc["complete_" + prop] = completeFunction;
  40.     mc.addEventListener(Event.ENTER_FRAME, makeMotionFunction(mc, prop));
  41. }
  42.  
  43. function makeMotionFunction(mc:MovieClip, prop:String):Function {
  44.     return function(evt:Event){
  45.         if (mc["time" +prop] <= mc["duration"+ prop]){
  46.            mc[prop]=mc["ease" + prop](mc["time" + prop], mc["begin" +prop], mc["change"+ prop], mc["duration"+ prop]);
  47.         } else {
  48.             var completeFunction:Function = mc["complete_"+prop];
  49.             if (completeFunction!=null){
  50.               completeFunction();
  51.             }
  52.             mc.removeEventListener(Event.ENTER_FRAME, arguments.callee);
  53.             // see how long the tween took
  54.             //trace(mc["time"+prop]);
  55.         }
  56.            mc["time" + prop] = (getTimer() - mc["startTime" + prop]) / 1000;
  57.     };
  58. }
  59. /*
  60. WARNING: This code was written for fun. Use at your own risk.
  61. */

This code uses a ~35 line tweening engine. I wrote this for fun a few weeks back. It works by creating dynamic variables on the MovieClip that it tweens. The dynamic variables and the square bracket syntax cause it to be pretty slow. Definitely not a substitute for a real tweening engine... like TweenLite.

I wouldn't recommend using this for anything other than playing around. See warning page for more info.

Also posted in dynamic, motion | Leave a comment