1

My question is, which one is better to use? How to properly use a switch statement. Should I use variables or not etc. I thank you in advance for your reply. "random text because I need a lot of explanation or else it won't let me post."

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

or

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

What I finally did (edit)

Final result, thanks all

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
                        ));
4

4 回答 4

1

You could also store all the values in some arrays. and access them via index.

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

You could even make it a two dimensional array, but I thought an array for each x, y, and z was simple, and faster than storing objects in an array.

A really good (and fast) option, would be to use Vectors, with the quick notation:

var lvlx:Vector.<int> = new <int>[50, 51, 52, 53, 54, 55, 56, 57];
于 2012-06-21T23:30:26.620 回答
1

Another option might be (untested code):

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));
于 2012-06-21T23:42:22.867 回答
0

There's not much of a difference in your case, but I would go with the latter. There is less code duplication this way (i.e. what happens if you want to change the parent of the level, so instead of stage.addChild you have someContainer.addChild, with the first method you would have to replace every instance of stage with someContainer vs. the second method replacing in only place).

If you're worried about lines of code, you can store the variables on one line:

x = 57; y = 107; z = 157;
于 2012-06-21T23:03:23.910 回答
0

As an entirely different approach, I would make a Level class that has the properties x, y and z and the method load.

Then I'd create instances of all the levels that are in the game with the appropriate values and load whichever one is relevant.

Sample:

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.
    }
}

Then just create all your levels and store them in an Array:

var levels:Array = [
    new Level(50, 100, 120),
    new Level(65, 70, 12)
];

Which means it's easy to load whichever level you want now by just having something like this:

function loadLevel(lvl:int):Level
{
    var level:Level = levels[lvl];
    level.load();

    return level;
}
于 2012-06-21T23:14:42.127 回答