I'm currently learning game development with C# and XNA and my current assignment is to create a Pac-Man clone.
The game is partially tile based, which means that the level itself is built out of an array of tiles, but Pac-Man and the ghost just have a position to be drawn at. I've got the basic functionality down, such as building the level with a string and drawing Pac-Man and the ghost on the screen.
Pac-Man is currently represented by two positions: one is where he is in relation to the array of tiles (a Point, for example 9, 11), and one where the sprite is drawn (which is a Vector2). However, when trying to make the player be able to move Pac-Man (and Pac-Man is supposed to "glide" between tiles, not jump between them) it seems that his graphical position can not keep up with his tile position. It sort of lags behind.
This is hard to describe but here's some code to look over:
public PacMan(): base()
{
texture = Game1.pacManTexture;
tilePosition = new Point(9, 11);
position = new Vector2(tilePosition.X * spriteSize.X, tilePosition.Y * spriteSize.Y);
That is the constructor for Pac-Man.
else if (tileManager.playArea[tilePosition.X, tilePosition.Y - 1] is Tile) // if the tile above Pac-Man is of the type Tile...
{
tilePosition.Y -= 1; // decrement tilePosition by 1
}
That is what happens if the player presses the Up key.
Vector2 tilePos = new Vector2(tilePosition.X * spriteSize.X, tilePosition.Y * spriteSize.Y);
Vector2 direction = tilePos - position;
if (direction.Length() != 0)
direction.Normalize();
position += direction;
And that is where the faulty magic happens. The graphical position of Pac-Man (the sprite) is being updated.
Rectangle pacManRectangle = new Rectangle((int)position.X, (int)position.Y, spriteSize.X, spriteSize.Y);
Game1.spriteBatch.Draw(texture, pacManRectangle, new Rectangle(currentFrame.X * spriteSize.X, currentFrame.Y * spriteSize.Y, spriteSize.X, spriteSize.Y), Color.White);
And lastly, that happens in the Draw method.
If you need more information, just say so.