您可以使用技巧来链接调用而不修改您的 Object :
var shape : Shape = new Shape;
// Chain property init
Initializer.init(shape).x(100).y(100).alpha(.5);
// Chain function call
Initializer.init(shape.graphics).beginFill( 0xFF0000 ).drawCircle( 100, 100, 50).endFill().beginFill( 0xFFFFFF ).drawCircle( 100, 100, 10).endFill();
addChild(shape);
和初始化类:
package
{
import flash.utils.Proxy;
import flash.utils.flash_proxy;
public dynamic class Initializer extends Proxy
{
// To avoid new instance
private static var _instance : Initializer = new Initializer(null);;
// Current target Object
private var _target : Object;
// Constructor
public function Initializer(target : Object){
_target = target;
}
// Call it to avoid new Initizer instance
public static function init(target : Object) : Initializer{
_instance._target = target;
return _instance;
}
// Catch function call and return initializer to chain call
override flash_proxy function callProperty(name:*, ... rest):* {
if(_target)
{
// Emulate function setter
if(_target.hasOwnProperty(name) && !(_target[name] is Function))
_target[name] = rest[0];
// If not a property, call as a classic function
else
_target[name].apply(_target, rest);
}
return this;
}
}
}
这只是为了好玩,因为代理调用为每次调用添加了非常小的时间,如果您想非常频繁地使用它(例如:每帧 10000),使用经典方法会更快。
您还可以使用“with”关键字(请注意构造函数后的 ; 字符):
var tf : TextField = new TextField(); with(tf) {
text = "Hello";
alpha = .5;
setTextFormat( new TextFormat( "Verdana", 16, 0xFF0000) );
}
或者当值相同时链接:
var text = new TextObjectThing(0, 0, 500, "Text");
text.scrollFactor.x = text.scrollFactor.y = 0;