使用Array来跟踪行和列

时间:2013-01-05 22:30:01

标签: java

我正在通过xml为游戏加载我的等级。

我有一个测试"名称"并相应地添加精灵。

我有一张地图,其瓷砖宽度和高度均为80by80。地图行和列是6x10。

我正在尝试找到一种方法来跟踪水平装载程序在加载切片时所处的行和列,因为我想用坐标做特定的事情。

我曾想过使用2d数组,但我不确定在这种情况下我会怎么做。

有人可以帮我解决这个问题吗?

编辑:

这是我尝试过的。

创建行和列数组

 int row[] = new int[6];
 int col[] = new int[10];

现在这里是我被困住的地方,我不知道如何判断代码何时切换并使用不同的行。例如..

if (name.equals(TAG_ENTITY_ATTRIBUTE_TYPE_unwalkable)) {
    tile = new Tile(x, y, this.tUnwalkable_tile,
            activity.getVertexBufferObjectManager());
    tileList.add(tile);
    tile.setTag(1);

    /*
     * Body groundBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld,
     * tile, BodyType.StaticBody, wallFixtureDef);
     */
    gameScene.getChildByIndex(SECOND_LAYER).attachChild(tile);
    Log.e("Tile", "Unwalkable_Tile");
    return;
} else if (name.equals(TAG_ENTITY_ATTRIBUTE_TYPE_Blue_Tile)) {
    tile = new Tile(x, y, this.blue,
            activity.getVertexBufferObjectManager());
    tile.setTag(0);
    this.tileList.add(tile);
    gameScene.getChildByIndex(SECOND_LAYER).attachChild(tile);
    return;

} else if (name.equals(TAG_ENTITY_ATTRIBUTE_TYPE_Red_Tile)) {
    tile = new Tile(x, y, this.red, activity.getVertexBufferObjectManager());
    tileList.add(tile);
    tile.setTag(0);
    gameScene.getChildByIndex(SECOND_LAYER).attachChild(tile);
    return;
} else if (name.equals(TAG_ENTITY_ATTRIBUTE_TYPE_Pink_Tile)) {
    tile = new Tile(x, y, this.pink,
            activity.getVertexBufferObjectManager());
    tileList.add(tile);
    tile.setTag(0);
    gameScene.getChildByIndex(SECOND_LAYER).attachChild(tile);
    return;
} else if (name.equals(TAG_ENTITY_ATTRIBUTE_TYPE_Yello_Tile)) {
    tile = new Tile(x, y, this.yellow,
            activity.getVertexBufferObjectManager());
    tileList.add(tile);
    tile.setTag(0);
    gameScene.getChildByIndex(SECOND_LAYER).attachChild(tile);
    return;

    }

如何告诉它保持行[1]直到到达col [10]?

然后切换到行[2]并保持在那里直到再次到达col [10]?

1 个答案:

答案 0 :(得分:0)

从设计角度来看,您的关卡加载程序应该只是加载关卡。无论你想做什么神奇的转变都可以,而且很可能应该单独处理。

所以只需让你的关卡加载器创建一个二维数组...无论它在读什么。你正在阅读一套扁平的瓷砖元素吗?然后计算读取的元素数量。在任何给定点,您的偏移量为:

 x = count % 10;
 y = count / 6; 

如果封装元素中包含每一行,请计算行数和列数。同样的想法。

现在你有一个二维数组(或一些封装它的对象)。你可以做任何你想要的转换。如果你想在屏幕空间方面这样做,而是将每个计数乘以80。

编辑:从上面的编辑中,看起来你正在声明单独的行和列数组,你可能不想这样做。它在逻辑上更像是:

int[][] tiles = new int[6][];
for (int i = 0; i < 6; i++) {
  tiles[i] = new int[10];  
}

然后,在你的加载器中,在某处定义一个计数器(一次)。

int counter = 0;

您将在以下时间切换行:

counter > 0 && (counter++) % 10 == 0 

但是对于2d数组,您可以将其视为具有坐标,如上所述:

 x = counter % 10;
 y = counter / 6; 

最后,你有一个tile [] []变量,它包含所有的tile数据。所以你可以说:

tiles[x][y] = <whatever data you need to store>

一旦完成,请记得增加计数器。