我的问题是,哪一个更好用?如何正确使用switch语句。我是否应该使用变量等等。我提前感谢您的回复。 "随机文字,因为我需要大量的解释,否则它不会让我发帖。"
switch(level_id)
{
case 1:
stage.addChild(new lvl(50, 200, 100, level_id));
break;
case 2:
stage.addChild(new lvl(50, 200, 100, level_id));
break;
case 3:
stage.addChild(new lvl(50, 200, 100, level_id));
break;
case 4:
stage.addChild(new lvl(50, 200, 100, level_id));
break;
case 5:
stage.addChild(new lvl(50, 200, 100, level_id));
break;
case 6:
stage.addChild(new lvl(50, 200, 100, level_id));
break;
case 7:
stage.addChild(new lvl(50, 200, 100, level_id));
break;
default:
break;
}
或
switch(level_id)
{
case 1:
x = 50; y = 200; x = 100;
break;
case 2:
x = 50; y = 200; x = 100;
break;
case 3:
x = 50; y = 200; x = 100;
break;
case 4:
x = 50; y = 200; x = 100;
break;
case 5:
x = 50; y = 200; x = 100;
break;
case 6:
x = 50; y = 200; x = 100;
break;
case 7:
x = 50; y = 200; x = 100;
break;
default:
break;
}
stage.addChild(new lvl(x, y, z, level_id));
我最终做了什么(编辑)
最终结果,谢谢所有
var config:Object = {
"1":{ "paddWidth":50, "blockWidth":200, "blockHeight":100 },
"2":{ "paddWidth":50, "blockWidth":200, "blockHeight":100 },
"3":{ "paddWidth":50, "blockWidth":200, "blockHeight":100 },
"4":{ "paddWidth":50, "blockWidth":200, "blockHeight":100 },
"5":{ "paddWidth":50, "blockWidth":200, "blockHeight":100 },
"6":{ "paddWidth":50, "blockWidth":200, "blockHeight":100 },
"7":{ "paddWidth":50, "blockWidth":200, "blockHeight":100 },
"8":{ "paddWidth":50, "blockWidth":200, "blockHeight":100 }
};
stage.addChild(new lvl(
config[level_id].paddWidth,
config[level_id].blockWidth,
config[level_id].blockHeight,
level_id
));
答案 0 :(得分:1)
您还可以将所有值存储在某些数组中。并通过索引访问它们。
var lvlx:Array = [50, 51, 52, 53, 54, 55, 56, 57];
var lvly:Array = [100, 101, 102, 103, 104, 105, 106, 107];
var lvlz:Array = [150, 151, 152, 153, 154, 155, 156, 157];
var lvlIndex:int = level_id - 1;
stage.addChild(new lvl(lvlx[lvlIndex], lvly[lvlIndex], lvlz[lvlIndex]));
你甚至可以把它变成一个二维数组,但我认为每个x,y和z的数组都很简单,并且比在数组中存储对象更快。
一个非常好(和快速)的选项,就是使用向量,快速表示法:
var lvlx:Vector.<int> = new <int>[50, 51, 52, 53, 54, 55, 56, 57];
答案 1 :(得分:1)
另一个选项可能是(未经测试的代码):
var config:Object = {
"1":{ "x":50, "y":100, "z":150 },
"2":{ "x":51, "y":101, "z":151 },
"3":{ "x":52, "y":102, "z":152 },
"4":{ "x":53, "y":103, "z":153 },
"5":{ "x":54, "y":104, "z":154 },
"6":{ "x":55, "y":105, "z":155 },
"7":{ "x":56, "y":106, "z":156 },
"8":{ "x":57, "y":107, "z":157 }
};
x = config[level_id].x;
y = config[level_id].y;
z = config[level_id].z;
stage.addChild(new lvl(x, y, z));
答案 2 :(得分:0)
你的情况没有太大区别,但我会选择后者。这种方式的代码重复较少(例如,如果您想要更改级别的父级会发生什么,所以不是stage.addChild
而是someContainer.addChild
,而第一种方法则需要替换每个实例使用someContainer的阶段与仅替换第二种方法的阶段。)
如果您担心代码行,可以将变量存储在一行:
x = 57; y = 107; z = 157;
答案 3 :(得分:0)
作为一种完全不同的方法,我会创建一个Level
类,其中包含属性x
,y
和z
以及方法load
。< / p>
然后,我将使用适当的值创建游戏中所有级别的实例,并加载相关的任何级别。
样品:
public class Level
{
public x:int;
public y:int;
public z:int;
public function Level(x:int, y:int, z:int)
{
this.x = x;
this.y = y;
this.z = z;
}
public function load(stage:Stage):void
{
// This is where the Level will do resource consuming stuff, like get
// added to the stage, create level elements, etc.
}
}
然后只需创建所有级别并将它们存储在数组中:
var levels:Array = [
new Level(50, 100, 120),
new Level(65, 70, 12)
];
这意味着只需要这样的东西就可以轻松加载你想要的任何级别:
function loadLevel(lvl:int):Level
{
var level:Level = levels[lvl];
level.load();
return level;
}