2013-01-05 14 views
0

我正在通过xml加载我的游戏级别。使用Array来记录行和列

我有一个循环,测试“名称”,并相应地添加精灵。

我有一张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

是否有任何理由水平加载器本身不能跟踪它加载哪个行和列?它肯定知道它在哪里加载数据。 – Mike

+0

是的,我创建了水平加载器它读取一个XML文件。如果需要,我会发布加载器代码。 –

回答

0

从设计的角度来看,您的级别加载器应该只是加载关卡。无论你想做什么的魔法变换都可以,而且很可能应该单独处理。

所以,只要你的级别加载器创建一个二维数组...无论它正在阅读。你正在阅读一组平面元素吗?然后统计读取的元素数量。在任何给定的点上,您的偏移量为:

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

如果您在封装元素中有每行,请计算行数和列数。同样的想法。

现在你已经有了一个2d数组(或一些封装它的对象)。你可以做任何你想要的变换。如果你想要这个屏幕空间,而不是乘以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 

但随着二维数组,你可以真的只是把它看作有坐标,正如我上面提到:

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

最后,你有一个tile [] []变量来存放所有的tile数据。所以你可以这样说:

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

并且记住一旦你完成就增加计数器。

+0

我添加了一些我以上试过的代码。你也可以给我一个你的意思吗? –

+0

你不想要两个单独的数组......你想要一个二维数组。 – James

+0

就像coords [] []。但是,你能否给我一个你的意思的例子? –