2012-06-21 23 views
1

我的问题是,哪一个更好用?如何正确使用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 
         )); 
+1

我会反对完全编辑问题,对于在变更之前没有看到问题的人来说,答案将不那么重要。 –

+1

这不再是个问题,也不会帮助其他人前来 –

+0

好吧,我会将其更改回来,对不起 – brace110

回答

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)); 
+1

你忘了“=”; var config:Object = { – brace110

+0

谢谢@ user1464312 - 刚刚添加它。 – artlung

0

没有太多的差别,你的情况,但我会选择后者。这种方式的代码重复次数更少(即,如果要更改关卡的父级,会发生什么情况,因此,而不是stage.addChild,您有someContainer.addChild,第一种方法是必须用someContainer替换stage的每个实例,而第二种方法只替换)。

如果你担心行代码,你可以在变量存储在同一行:

x = 57; y = 107; z = 157; 
+0

不要将变量存储在一行上!让我哭了:'( –

0

作为一个完全不同的方法,我会做一个Level类具有性能xyz和方法load

然后,我会创建游戏中所有关卡的实例,并使用相应的值并加载相关的值。

样品:

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; 
} 
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];