从其他类访问“currentState”?
问题描述:
我在Actionscript 3中做了一个小应用程序。在我的initApp.as中,我创建了另一个需要编辑currentState的类,它只能从主.as(initApp.as)访问。我找到了一个解决方案,以便可以从我的其他类中访问currentState属性:Application.application.currentState。从其他类访问“currentState”?
然而,这不是一个好的解决方案,因为它太多了耦合类..有没有更好的方式来编辑其他类的currentState?
答
我建议你使用事件和*调度程序。例如:
在InitApp.as
Dispatcher.instance.addEventListener(StateChangeEvent.STATE_CHANGE, onStateChange);
protected function onStateChange(e:StateChangeEvent):void
{
this.currentState = e.newState;
// perhaps dispatch another state change event that all views can listen for?
}
在你其他类
Dispatcher.instance.dispatchEvent(new StateChangeEvent(StateChangeEvent.STATE_CHANGE, newState);
,其中调度是独立的,并StateChangeEvent是一个newState属性自定义事件。祝你好运!