at the moment I am developing a game in c# and im a little stuck.
I need to draw the tile map. I read the tile map in using xml and store each tile in a list with its x and y position. but Im having a bit of trouble with the draw method.
public void Draw(SpriteBatch theBatch)
{
int tileWidth = 32;
int tileHeight = 32;
int numTiles = 100;
while (j < numTiles)
{
tileMap[j].Position.X *= tileWidth;
tileMap[j].Position.Y *= tileHeight;
theBatch.Draw(mSpriteTexture, tileMap[j].Position, Color.White);
Console.WriteLine("tile drawn");
Console.WriteLine("x = " + tileMap[j].Position.X);
Console.WriteLine("y = " + tileMap[j].Position.Y);
Console.WriteLine(" i = " + j);
j++;
}
}
I thought this would work and the tile map appears on screen but then quickly disappears, any ideas what I am doing wrong? sorry if its really silly.
thanks, Iain
Looks like the Case of the Mistranslated For Loop 🙂
jis not declared in the method you posted, so I’m assuming it’s an instance field of typeintinitialised to 0. It needs to be re-initialised to 0 every time before the loop, otherwisej < numTileswill be false on every frame except the first (so the tiles will only get drawn on the first frame — since the screen is erased at the beginning of each frames, the tiles disappear if they aren’t being constantly re-drawn).Also, why are you multiplying the position of each tile by it’s size (on every frame, too!)? This would quickly cause the tiles to be drawn off-screen after a few frames as their positions would increase exponentially with the number of frames drawn. If the tiles aren’t moving, their positions should not need to be modified at all (and if they are moving, that should be done in the Update method, not Draw).
Finally, make sure you comment out the Console.WriteLine calls when you’re done debugging, since writing to the console (several times per second) is rather slow.