【问题标题】:2D Tile System not behaving properly2D 瓷砖系统无法正常运行
【发布时间】:2011-04-10 00:56:23
【问题描述】:

我正在尝试基于 Snake 示例制作一个 2-D 平铺系统。原来的 mTileGrid 还在,但是有一个新的数组:mMapGrid。重点是在每个更新周期将数据从 mMapGrid 复制到 mTileGrid,从而模拟比当前屏幕更大的区域。

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

在地图网格中设置图块 公共无效 setMapTile(int tileindex,int 行,int 列) { 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 + " and TopLeftCorner.row is " + 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)

@覆盖 公共无效onDraw(帆布画布){ 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);

        }
    }

}

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

【问题讨论】:

标签: android map grid 2d tile


【解决方案1】:

乍一看,我会说您不应该在同一个 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 中实际上不存在的索引。

【讨论】:

  • 现在它只显示屏幕的大致上半部分并带有瓷砖
  • 知道为什么它的行为如此奇怪吗?
  • mXTileCount 和 mYTileCount 是 mMapGrid 还是 mTileGrid 的维度?如果它们是 mTileGrid 的尺寸,则必须在 for 循环中将 TopLeftCorner 坐标添加到它们。此外,您从 mMapGrid 获取 tileIndex 的行会将 TopLeftCorner 添加到 x 和 y,即使它们已经以该值开头。
  • 我想最好在 x 和 y 设置为零并限制在 mTileGrid 的宽度和高度的情况下启动 for 循环。然后在 setTileGrid 中使用 x 和 y 代替 countrow 和 countcolumn。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 2016-11-19
  • 1970-01-01
相关资源
最近更新 更多