I have created this game that moves the player like in the old final fantasy games, so they are snapped to the grid. The player moves 32 pixels and stops, or keeps moving 32 px each time when the button is held down. The map is stuttering during movement whether holding button down or just pushing movement direction once.
I have 3 map layers, I draw the bottom, middle, player, then top layer in that order each update cycle.
Here is an image of the problem, its stuttering and showing a 1px line each map redraw when moving
Here is the draw calls to the map manager object
tmm.Draw((int)mapLayerName.bottom, _spriteBatch);
tmm.Draw((int)mapLayerName.middle, _spriteBatch);
player.anim.Draw(_spriteBatch);
tmm.Draw((int)mapLayerName.top, _spriteBatch);
And here is the draw code
public void Draw(int layerInt, SpriteBatch spriteBatch)
{
for (var i = 0; i < _map.Layers[layerInt].data.Length; i++)
{
int gid = _map.Layers[layerInt].data[i];
// Empty tile, do nothing
if (gid == 0)
{
}
else
{
int tileFrame = gid - 1;
// Print the tile type into the debug console.
// This assumes only one (1) `tiled tileset` is being used, so getting the first one.
var tile = _map.GetTiledTile(_map.Tilesets[0], _tileset, gid);
int column = tileFrame % _tilesetTilesWide;
int row = (int)Math.Floor((double)tileFrame / (double)_tilesetTilesWide);
float x = (i % _map.Width) * _map.TileWidth;
float y = (float)Math.Floor(i / (double)_map.Width) * _map.TileHeight;
Rectangle tilesetRec = new Rectangle(_tileWidth * column, _tileHeight * row, _tileWidth, _tileHeight);
spriteBatch.Draw(_tilesetTexture, new Rectangle((int)x, (int)y, _tileWidth, _tileHeight), tilesetRec, Color.White);
}
}
}
Are you repositioning the tiles every update? Is yes you should consider using a 2d camera. It may fix some of the weird stuff you are seeing.
Yes I am drawing the map each draw cycle, I have a camera on the player. Are you saying I should only draw the map once?
this.camera.Position = player.getPosition;
this.camera.Update(gameTime);
It only seems to be a problem for the blue and green tiles.
Maybe there is something offset with the graphics for those 2 tiles?
The blue tiles are the default background color, not part of the the tiled map, but the lines on the blue are invisible collision blocks for bounding, both he blue and green are on the bottom layer, the other tiles that show no issue are on the middle and top layers.
I have found the issue, it looks like the tileset I am using is not pixel perfect, one set of tiles is 1 pixel off in 2 directions, and its only used on the bottom layer. I corrected the tileset and it seems to be working.
Thank you.
It not the problem that you are drawing the tiles during every frame. I was confused why you need to recalculate the position of the tile every update. You should be able to draw the tile at the exact position everytime. The camera should be able to handle everything else.