11.13.动态生成States和Transitions

11.13.1. 问题
我需要动态生成新的states 和transitions。
11.13.2. 解决办法
创建新的State 和Transition 对象,添加它们的属性,把它们添加到每个UIComponent 对象定义的states 和transition 数组中。
11.13.3. 讨论
一般情况下是不能频繁地创建新的states 和transition 的。但是在某些情况下是很有必要的。

比如模板组件。因为每个UIComponent 对象有一个states 数组和transition 数组,包含所有的State 和Transition 对象,创建新的states 和transitions 只需要简单的定义State 或Transition并添加到对应的数组中即可:
+展开
-ActionScript
var state:State = new State();
var button:Button = new Button();
button.label = "LABEL";
var addChild:AddChild = new AddChild(vbox, button);
state.overrides = [addChild];
state.name = "buttonState";
states.push(state);

这里被重写的数组是将被使用的State 的重写列表,就是,哪些功能将被定义,哪些特定的State 属性将被定义,比如任何SetProperty 或SetStyle 操作,所有为State 定义的重写都是实现了IOverride 接口,一般包含下列:
AddChild
RemoveChild
SetEventHandler
SetProperty
SetStyle

(For more information on the IOverride interface, see Recipe 11.15.)
下面是完整的代码,注意创建Transition 对象,添加属性,指定toState 和fromState 属性的这些方法:
+展开
-XML
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxmlwidth="400height="300">
<mx:Script>
<![CDATA[
import mx.controls.Button;
import mx.states.AddChild;
import mx.states.State;
import mx.states.Transition;
import mx.effects.Move;
public function addTransitions():void {
var transition:Transition = new Transition();
var move:Move = new Move();
move.duration=400;
move.target = vbox;
transition.fromState = "buttonState";
transition.toState = "*";
transition.effect = move;
transitions.push(transition);
}
public function addState():void {
var state:State = new State();
var button:Button = new Button();
button.label = "LABEL";
var addChild:AddChild = new AddChild(vbox, button);
state.overrides = [addChild];
state.name = "buttonState";
states.push(state);
}

]]>
</mx:Script>
<mx:VBox id="vbox"/>
<mx:Button click="addTransitions()label="new transition"/>
<mx:Button click="addState()label="new state"/>
<mx:Button click="currentState='buttonState'label="change
state
"/>

</mx:VBox>

加支付宝好友偷能量挖...


评论(0)网络
阅读(142)喜欢(0)flash/flex/fcs/AIR