访问" currentState的"从其他类? [英] Access "currentState" from other classes?
问题描述
我做在我initApp.as我已经创建了另一个类,需要编辑在currentState,这是主要的。至于(initApp.as)仅在访问ActionScript 3的一个小应用程序。我发现了一个解决方案,这样我可以从我的其他类达到currentState属性:Application.application.currentState
I'm making a little application in Actionscript 3. In my initApp.as I have created another class which needs to edit the currentState, which is only accessible from the main .as (initApp.as). I found a solution such that I can reach the currentState property from my other class: Application.application.currentState.
但是,这是不是一个很好的解决方案,因为它夫妻的类太多了..有从其他类编辑在currentState更好的办法?
This is however not a good solution as it couples the classes too much.. is there a better way of editing the currentState from other classes?
推荐答案
我会建议你使用事件和中央调度。例如:
I would suggest you use events and a central dispatcher. For example:
在InitApp.as
In 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);
,其中Dispatcher是一个独立的,并StateChangeEvent是一个newState属性自定义事件。祝你好运!
where Dispatcher is a singleton and StateChangeEvent is a custom event with a newState property. Good luck!
这篇关于访问" currentState的"从其他类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!