#### Archived

This topic is now archived and is closed to further replies.

# problem with scrolling tile based game...

This topic is 5315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

this is how i draw the map:
 for (int y=yoffset/TileSize;y<((yoffset+ScreenHeight)/TileSize)+1;y++)
{
for(int x=xoffset/TileSize;x<((xoffset+ScreenWidth)/TileSize)+1;x++)
{
Tiles[x][y].PosX=x;
Tiles[x][y].PosY=y;
DrawTile(Tiles[x][y].srcX,Tiles[x][y].srcY,x*TileSize-xoffset,y*TileSize-yoffset,TileSize,g_pTileTexture,D3DCOLOR_RGBA(255,255,255,255),Tiles[x][y].Rotation);
}
}

what is important in all this code, it''s the -xoffset and -yoffset. now, for scrolling, i''m increasind/reducing the offset. now, i have a problem deretminating, when i reached the end of the map... for scrolling to the right i do this: if (xoffset<(MapWidth*TileSize)-ScreenWidth) xoffset++; but this is not working (the game crash on me...) and i dont know why... Thanks in advance.

##### Share on other sites
Ilankt - If you wish your game world to scroll beyond its edge by wrapping around to the beginning, one thing I notice is -- I see no means to do so in your code, which would definately cause it to crash (due to out of bounds array access).

There may be more involved, but for starters I suggest adding a macro or inline function to do something like the following:

inline int WrapX (int xPos)
{
return (xPos % xSize);
}

...where xPos is the x index of your Tiles[x][y] array and xSize is the size of the x component of the tile array. Repeat for yPos and ySize.

Now you can address Tiles by Tiles[WrapX(x)][WrapY(y)] instead of Tiles[x][y] and not worry about accessing out of array bounds.

Good luck.

##### Share on other sites
THANKS!!!
this helps alot!
even that my problem is solved (by you ), I really wanted to know what is the problem with the code...
but, never mind now, at least i dont have any problems now (yet).

##### Share on other sites
Ilankt -- Great...

To get it working for negative offsets, try modifying the macro/inline function (Boldface indicates the change):

inline int WrapX (int xPos)
{
return ((xSize + xPos) % xSize);
}

Alternatively, if you want to keep xoffset and yoffset from going negative in the first place, try applying this logic to them instead of the x and y indeces. Change this:

if (xoffset<(MapWidth*TileSize)-ScreenWidth)
xoffset++;

To this:

if (xoffset<(MapWidth*TileSize)-ScreenWidth)
xoffset = ((xoffset_max + xoffset + 1) % xoffset_max);

where xoffset_max is the largest xoffset value + 1.

You actually only need to add the max when scrolling left or up (to prevent negative values), btw, but the mod (%) is always needed.

Peace.

EDIT: Fixed a couple of typos.

[edited by - BrianMJC on August 3, 2003 5:24:06 PM]