Set Properties Function AS3

Here’s a simple, but very useful function that sets the properties of a specified target, with properties of an object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* Set properties from object
*/

function setProps($target:Object, $props:Object):void
{
  for(var p:String in $props)
  {
    $target[p] = $props[p];
  }
}

// Example use
import flash.text.TextFormat;

var props:Object = {leading:12, color:0xFF0000, size:24};
var tf:TextFormat = new TextFormat();

setProps(tf, props);    // Set the properties of tf with props object

trace(tf.leading + ", " + tf.color + ", " + tf.size);     // check the result
Posted in AS3