12.2.建立一个自定义效果
12.2.1 问题
我想创建一个既可以在MXML 也可以在ActionScript 内使用的自定义效果。
12.2.2 解决办法
创建一个继承Effect 的类以及任何你想要再实例化的时候传入实例的getter 、setter 属性。然后再创建一个继承EffectInstance 的实例类,该类即是实际播放变化效果的类。
12.2.3 讨论
在Flex 框架中,每个效果有两个元素组成:一个Effect 和一个EffectInstance。Effect 创建EffectInstance 并传递属性给它们。这种责任分配允许你轻易地创建在多个对象上播放的效果。
要这样做的话,首先定义一个TestEffect 类(随后创建EffectInstance 的工厂),设置它们的属性,并且调用每个实例的play 方法:
注意前面的代码,在initInstance 方法里,创建EffectInstance 类的实例。TestInstance 类申明为TestInstance 类型,并且TestInstance 的属性都设置为TestEffect 工厂的属性值。这让你可以通过TestEffect 工厂一次性设置每个TestInstance 实例的属性。
由TestEffect 工厂生成的TestInstance 类代码如下:
创建TestInstance 的时候,每个TestInstance 的target 属性都由TestEffect 工厂类设置。这保证如果传入多个目标对象给Effect 类的targets 属性,TestEffect,一个TestInstance 的实例,将会创建并且在每个对象上播放。TestInstance 的color 和alpha 属性将会在实例创建的时候由TestEffect 的initInstance 方法设置。
TestInstance 类里覆盖的play 方法包含了显示逻辑以改变分配给TestInstance 的目标UIComponent。
我想创建一个既可以在MXML 也可以在ActionScript 内使用的自定义效果。
12.2.2 解决办法
创建一个继承Effect 的类以及任何你想要再实例化的时候传入实例的getter 、setter 属性。然后再创建一个继承EffectInstance 的实例类,该类即是实际播放变化效果的类。
12.2.3 讨论
在Flex 框架中,每个效果有两个元素组成:一个Effect 和一个EffectInstance。Effect 创建EffectInstance 并传递属性给它们。这种责任分配允许你轻易地创建在多个对象上播放的效果。
要这样做的话,首先定义一个TestEffect 类(随后创建EffectInstance 的工厂),设置它们的属性,并且调用每个实例的play 方法:
+展开
-ActionScript
package oreilly.cookbook{
import mx.effects.Effect;
import mx.effects.IEffectInstance;
import mx.events.EffectEvent;
import oreilly.cookbook.TestInstance;
public class TestEffect extends Effect
{
public var color:uint;
public var alpha:Number;
public function TestEffect(target:Object=null) {
// call the base constructor of course
super(target);
// set our instance class to the desired instance type
instanceClass = TestInstance;
}
override protected function
initInstance(instance:IEffectInstance):void {
trace(" instance initialized ");
super.initInstance(instance);
// now that we've instantiated our instance, we can set
its properties
TestInstance(instance).color = color;
TestInstance(instance).alpha = alpha;
}
override public function getAffectedProperties():Array {
trace(" return all the target properties ");
return [];
}
override protected function
effectEndHandler(event:EffectEvent):void {
trace(" effect ended ");
}
override protected function
effectStartHandler(event:EffectEvent):void {
trace(" effect started ");
}
}
}
import mx.effects.Effect;
import mx.effects.IEffectInstance;
import mx.events.EffectEvent;
import oreilly.cookbook.TestInstance;
public class TestEffect extends Effect
{
public var color:uint;
public var alpha:Number;
public function TestEffect(target:Object=null) {
// call the base constructor of course
super(target);
// set our instance class to the desired instance type
instanceClass = TestInstance;
}
override protected function
initInstance(instance:IEffectInstance):void {
trace(" instance initialized ");
super.initInstance(instance);
// now that we've instantiated our instance, we can set
its properties
TestInstance(instance).color = color;
TestInstance(instance).alpha = alpha;
}
override public function getAffectedProperties():Array {
trace(" return all the target properties ");
return [];
}
override protected function
effectEndHandler(event:EffectEvent):void {
trace(" effect ended ");
}
override protected function
effectStartHandler(event:EffectEvent):void {
trace(" effect started ");
}
}
}
注意前面的代码,在initInstance 方法里,创建EffectInstance 类的实例。TestInstance 类申明为TestInstance 类型,并且TestInstance 的属性都设置为TestEffect 工厂的属性值。这让你可以通过TestEffect 工厂一次性设置每个TestInstance 实例的属性。
由TestEffect 工厂生成的TestInstance 类代码如下:
+展开
-ActionScript
package oreilly.cookbook{
import flash.display.DisplayObject;
import mx.core.Container;
import mx.core.FlexShape;
import mx.core.UIComponent;
import mx.effects.EffectInstance;
public class TestInstance extends EffectInstance
{
public var alpha:Number;
public var color:uint;
public function TestInstance(target:Object) {
super(target);
}
override public function play():void {
super.play();
(target as DisplayObject).alpha = alpha;
var shape:FlexShape = new FlexShape();
shape.graphics.beginFill(color, 1.0);
shape.graphics.drawRect(0, 0, (target as
DisplayObject).width, (target as
DisplayObject).height);
shape.graphics.endFill();
var uiComp:UIComponent = new UIComponent();
uiComp.addChild(shape);
UIComponent(target).addChild(uiComp);
}
}
}
import flash.display.DisplayObject;
import mx.core.Container;
import mx.core.FlexShape;
import mx.core.UIComponent;
import mx.effects.EffectInstance;
public class TestInstance extends EffectInstance
{
public var alpha:Number;
public var color:uint;
public function TestInstance(target:Object) {
super(target);
}
override public function play():void {
super.play();
(target as DisplayObject).alpha = alpha;
var shape:FlexShape = new FlexShape();
shape.graphics.beginFill(color, 1.0);
shape.graphics.drawRect(0, 0, (target as
DisplayObject).width, (target as
DisplayObject).height);
shape.graphics.endFill();
var uiComp:UIComponent = new UIComponent();
uiComp.addChild(shape);
UIComponent(target).addChild(uiComp);
}
}
}
创建TestInstance 的时候,每个TestInstance 的target 属性都由TestEffect 工厂类设置。这保证如果传入多个目标对象给Effect 类的targets 属性,TestEffect,一个TestInstance 的实例,将会创建并且在每个对象上播放。TestInstance 的color 和alpha 属性将会在实例创建的时候由TestEffect 的initInstance 方法设置。
TestInstance 类里覆盖的play 方法包含了显示逻辑以改变分配给TestInstance 的目标UIComponent。
加支付宝好友偷能量挖...