我想保持完全控制我的比赛从主要影片剪辑和别的地方。但是,我不想通过构造函数传递它的实例也不做其子女的任何的 .parent 的参考啄。似乎过于workaroundish和不稳定的。
I want to keep total control over my game from the Main MovieClip and nowhere else. But I don't want to pass its instance through constructors neither do any .parent reference thingy from its children. Seems too workaroundish and unstable.
一个样本情况:
public class Main extends MovieClip {
public function Main() {
addChild(new MainMenu());
}
public function startGame():void {
trace("Game started");
}
}
public class MainMenu extends Sprite {
public function MainMenu() {
var option:Option = new Option(); // Some BitmapData library linkage
option.addEventListener(MouseEvent.CLICK, clicked);
addChild(option);
}
public function destroy():void {
// set null/dispose/removeChildren/removeEventListener/etc.
}
private function clicked(evt:MouseEvent):void {
// Should trigger startGame() here (how?) to keep the flow at Main,
destroy(); // since this has nothing to do with it anymore
}
}
我想对于一些单身样的解决方案,但没有静态类,在AS3中。它似乎不好的做法,从我研究了(是吗?)。
I wished for some Singleton-like solution, but there's no static class in AS3. And it seems bad practice from what I researched (or is it?).
嗯,我只是想从做的一切主要观点与典雅code 的或至少的正式做法的。我怎样才能做到这一点? (请告诉我这是可能的... :秒)
Well, I just want to do everything from Main perspective with elegant code or at least official practice. How can I do this? (Please, tell me this is possible... :s)
我觉得有2个方法,你可以考虑一下: 静态实例:
I think there are 2 more ways that You can think about : Static instance :
public static var main:Main;
public function Main() {
main = this;
...
}
和值:
public function MainMenu() {}
private function clicked(evt:MouseEvent):void {
Main.main.startGame();
}
或者用一些静态调度,如果你的应用程序是不是太大了:
Or use some static dispatcher if Your app is not too big :
public static var dsp:EventDispatcher = new EventDispatcher();
public function Main() {
dsp.addEventListener("StartGame",stageGame);
...
}
和在MainMenu的:
and in mainmenu :
private function clicked(evt:MouseEvent):void {
Main.dsp.dispatchEvent(new Event("StartGame"));
}
在这里,您可以随时创建其他类,将处理事件指派/听。
Here You can always create additional class that will handle event dispatching / listening .