2D瓷砖系统表现不正常

时间:2010-09-09 00:57:54

标签: android map grid 2d tile

我试图在Snake示例的基础上大量制作2-D拼贴系统。原始的mTileGrid仍然存在,但是有一个新的数组:mMapGrid。重点是在每个更新周期将数据从mMapGrid复制到mTileGrid,因此模拟大于直接屏幕的区域。

我写了更多的函数,我现在将简要解释一下......之后,问题出现了:

在地图网格中设置图块     public void setMapTile(int tileindex,int row,int column)     {      mMapGrid [row] [column] = tileindex;     }

在onDraw()中调用,将数据从mMapGrid移动到mTileGrid

private void CopyArrayData()
{
 //TODO: TAG
 int countrow, countcolumn;
 countrow = 0;
 countcolumn = 0;

Log.w(“PassNumbers”,“TopLeftCorner.column is:”+ TopLeftCorner.column +“和TopLeftCorner.row”是“+ TopLeftCorner.row”;

 for(int x = TopLeftCorner.column; x <= mXTileCount; x++)
 {
  for(int y = TopLeftCorner.row; y <= mYTileCount; y++)
  {
   countrow++;
   countcolumn++;


   if(countrow == mXTileCount)
   {
    countrow = 0;
   }
   if(countcolumn == mYTileCount)
   {
    countcolumn = 0;
   }




   int set = mMapGrid[y + TopLeftCorner.row][x + TopLeftCorner.column];
   if(set == SnakeView.GRASS)
   {
    setTile(set, countrow, countcolumn);
   }
   else
   {
    setTile(SnakeView.ROCK, countrow, countcolumn);
   }


   if(pass1 == false)
   {
    Log.w("TileGridAccess",("TileGrid Access: row" + countrow + " column" + countcolumn));
    Log.w("MapGridAccess","MapGrid Access: row" + (y + TopLeftCorner.row) + " column:" + (x + TopLeftCorner.column));
   }

  }

  pass1 = true;
 }



}

}

update()函数,此处调用setMapTile()

public void update() {
    if (mMode == RUNNING) {


            clearTiles();


            setMapTile(GRASS, 5, 5);
            setMapTile(GRASS, 5, 6);
            setMapTile(GRASS, 5, 7);
            setMapTile(GRASS, 5, 8);
            setMapTile(GRASS, 5, 9);



        mRedrawHandler.sleep(mMoveDelay);
    }

}

onDraw()函数:(与Snake中的原始TileView不变,除了因为我们有一个默认的tile而消除if大于零的检查)

@覆盖     public void onDraw(Canvas canvas){         super.onDraw(帆布);

    CopyArrayData();

    for (int x = 0; x < mXTileCount; x += 1) {
        for (int y = 0; y < mYTileCount; y += 1) {
                canvas.drawBitmap(mTileArray[mTileGrid[x][y]], 
                  mXOffset + x * mTileSize,
                  mYOffset + y * mTileSize,
                  mPaint);

        }
    }

}

问题是显示的图块间距不均匀,并且表现不正常。他们偶然散落在他们的行上。

1 个答案:

答案 0 :(得分:0)

乍一看,我会说你不应该在同一个for循环中递增countrow和countcolumn。这就是我认为它应该是这样的:

for(int x = TopLeftCorner.column; x <= mXTileCount; x++)
 {
  countcolumn++;
  for(int y = TopLeftCorner.row; y <= mYTileCount; y++)
  {
   countrow++;
  ...

编辑:

for(int x = 0; x <= mXTileCount; x++)
 {
  for(int y = 0; y <= mYTileCount; y++)
  {

   int set = mMapGrid[y + TopLeftCorner.row][x + TopLeftCorner.column];
   if(set == SnakeView.GRASS)
   {
    setTile(set, y, x);
   }
   else
   {
    setTile(SnakeView.ROCK, y, x);
   }
   ...

这样,函数循环遍历屏幕空间中的坐标,因此您不再需要countrow和countcolumn。而是从mMapGrid检索数据时添加TopLeftCorner。我假设TopLeftCorner指定当前在屏幕上显示地图的哪个部分。确保这些坐标保持有限,以便无法访问mMapGrid中实际不存在的索引。