views:

78

answers:

1

Hello and Welcome I am trying to make a 2-D tile system heavily based off of the Snake example. The original mTileGrid is still there, but there is a new array: mMapGrid. The point is to copy the data from mMapGrid to mTileGrid on each update cycle and therefore simulate an area larger than the immediate screen.

I wrote more functions, which I will now briefly explain... After that, the problem:

Set a tile in the map grid public void setMapTile(int tileindex, int row, int column) { mMapGrid[row][column] = tileindex; }

Called in onDraw(), moves the data from mMapGrid to 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;
 }



}

}

The update() function, with setMapTile() called here

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

}

The onDraw() function: (unchanged from the original TileView in Snake, except for the elimination of the if greater than zero check since we have a default 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);

        }
    }

}

The problem is that the tiles displayed are not evenly spaced and do not behave as they should. They are haphazardly scattered about their row.

A: 

At first glance i'd say you should not be incrementing both countrow and countcolumn in the same for-loop. This is how i think it should look like:

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

Edit:

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

This way the function loops through coordinates in screen space, so you don't need countrow and countcolumn anymore. Instead TopLeftCorner is added when data is retrieved from mMapGrid. I assume that TopLeftCorner specifies which part of the map is currently shown on the screen. Make sure that these coordinates stay limited so that there is no access to indices in mMapGrid that don't actually exist.

domihal
Did thatnow it only displays the roughly top half of the screen with tiles
dragonwrenn
dragonwrenn
any idea why its behaving so strangely?
dragonwrenn
Are mXTileCount and mYTileCount the dimensions of mMapGrid or mTileGrid? If they are the dimensions of mTileGrid you must add the TopLeftCorner coordinates to them in the for-loop. Also, the line where you get the tileIndex from mMapGrid adds TopLeftCorner to x and y even though they already start with that value.
domihal
I guess it's best to start the for-loop with x and y set to zero and limited to the width and height of mTileGrid. Then use x and y in setTileGrid instead of countrow and countcolumn.
domihal
unchanged from the Snake example: @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { mXTileCount = (int) Math.floor(w / mTileSize); mYTileCount = (int) Math.floor(h / mTileSize);
dragonwrenn