国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

12.2.建立一個自定義效果

2024-04-27 13:52:13
字體:
來源:轉載
供稿:網友
12.2.1 問題
我想創建一個既可以在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 ");
}
}
}

注意前面的代碼,在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);
}
}
}

創建TestInstance 的時候,每個TestInstance 的target 屬性都由TestEffect 工廠類設置。這保證如果傳入多個目標對象給Effect 類的targets 屬性,TestEffect,一個TestInstance 的實例,將會創建并且在每個對象上播放。TestInstance 的color 和alpha 屬性將會在實例創建的時候由TestEffect 的initInstance 方法設置。

TestInstance 類里覆蓋的play 方法包含了顯示邏輯以改變分配給TestInstance 的目標UIComponent。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延川县| 防城港市| 葵青区| 新疆| 安国市| 逊克县| 宣武区| 丹巴县| 修武县| 德保县| 祁门县| 渑池县| 阿巴嘎旗| 新安县| 眉山市| 遂溪县| 曲靖市| 隆子县| 内乡县| 津南区| 亳州市| 岳池县| 石家庄市| 当阳市| 新河县| 方正县| 株洲县| 浠水县| 绵阳市| 天气| 体育| 新竹市| 东乡族自治县| 平泉县| 兰溪市| 孟村| 莆田市| 洛阳市| 佳木斯市| 吴旗县| 新津县|