蓝天,你会拥有:
StaticUtil.wire( fromProperty:*, toProperty:*, usingExpression:Function );
并像这样使用它:
sprite1.x = 100;
sprite2.width = 200;
StaticUtil.wire( sprite1.x, sprite2.width, function(fromX:Number,toWidth:Number):void{ toWidth = fromX * 2; });
sprite.x = 150;
trace(sprite2.width); //300;
我发现在 actionscript 中动态覆盖 getter/setter 很可能是不可能的......这导致了我这个初步的解决方案:
public class WireableSprite extends Sprite implements IWire
{
private var _wire:IWire;
public function WireableSprite()
{
super();
}
public function wireTo( wireable:IWire ):void
{
_wire = wireable;
}
override public function set rotation(value:Number):void
{
super.rotation = value;
if( _wire ) _wire["rotation"] = value;
}
override public function set scaleX(value:Number):void
{
super.scaleX = value;
if( _wire ) _wire["scaleX"] = value;
}
override public function set scaleY(value:Number):void
{
super.scaleY = value;
if( _wire ) _wire["scaleY"] = value;
}
override public function set x(value:Number):void
{
super.x = value;
if( _wire ) _wire["x"] = value;
}
override public function set y(value:Number):void
{
super.y = value;
if( _wire ) _wire["y"] = value;
}
}
并像这样使用它:
myWireableSprite.wireTo( myOtherSprite );
这真的不是很灵活......
我的问题是,在我没有太多重复代码的情况下,有没有人知道一种更聪明、更灵活的方法?