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;

This entry was posted in Object, properties. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*