0

我正在尝试根据 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 是:" + 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 保持不变,除了消除了 if 大于零检查,因为我们有一个默认的 tile)

@Override public void onDraw(Canvas canvas) { super.onDraw(canvas);

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

        }
    }

}

问题是显示的图块间隔不均匀,并且表现不佳。他们随意地分散在他们的行中。

4

1 回答 1

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 中实际上不存在的索引。

于 2010-09-09T03:18:19.600 回答