Actionscript 3 何时使用状态模式?
我正在从事一个Flash项目,该项目最初有一个简单的动画模板,但现在已经发展到具有多个模板的不同“状态”(不在我的控制之下) 因此,我的更新(输入帧)循环现在看起来有点像这样:Actionscript 3 何时使用状态模式?,actionscript-3,Actionscript 3,我正在从事一个Flash项目,该项目最初有一个简单的动画模板,但现在已经发展到具有多个模板的不同“状态”(不在我的控制之下) 因此,我的更新(输入帧)循环现在看起来有点像这样: private function update():void { switch (state) { case "intro": switch(layoutState) { case "large-images": // do one a
private function update():void {
switch (state) {
case "intro":
switch(layoutState) {
case "large-images":
// do one animation
break;
case "thumbnails":
// do another animation
break;
case "text-on-top":
// do another animation
break;
}
break;
case "main":
switch(layoutState) {
case "large-images":
// do another animation
break;
case "thumbnails":
// do another animation
break;
case "text-on-top":
// do another animation
break;
}
break;
case "outro":
switch(layoutState) {
case "large-images":
break;
case "thumbnails":
break;
case "text-on-top":
break;
}
break;
}
switch(backgroundState) {
case "black":
// do something
break;
case "white":
// do something else
break;
}
}
我的初始化方法开始如下所示:
private function initalizeDescription() {
description = new Description();
switch(layoutState) {
case "large-images":
// do something to description here
break;
case "thumbnails":
// do something else to description here
if (backgroundState == "black") {
// do one thing
}
if (backgroundState == "white") {
// do another thing
}
break;
case "text-on-top":
// do something else to description here
break;
}
}
我对伪代码表示歉意,但真正的代码相当长
在这种情况下,使用状态模式会更好吗?如果是的话,有人能提供一个(简短的)代码示例,说明如何最好地实现它吗?你敢打赌这是一个使用状态模式的好机会!每当我需要开始嵌套switch语句时,我都会使用它,尤其是中推荐的实现 (很抱歉,我找不到本章的免费版本可供链接,但我认为这本书物有所值。)