I'm working on a tower defense game. I got everything setup and I can build tower no problems. What I do is check where is the mouse position and then divide the mouse position by 32 pixels (becuase its the Tile or Tower size) and convert the floating point to integer . and then I multiply by 32 pixels.
so lets say mouse position is X200 Y150. so 200/32 = 6.25 convert that to int removing the decimal so its 6 * 32 = 192 pixels on X axis. same thing for Y. I do this to make sure that I build towers every 32 pixel.
Now it works fine when I don't scroll the game. However when I start scrolling and try to build out of screen the position becomes inaccurate. I just want to know why does it become inaccuracy.
I think the way I'm doing the scrolling is wrong and that is why I'm getting wrong position.
Here is a video that explains my problem more.
[media]
[/media]
from 0:00 till 0:30 it will show that when I build the tower its accurate. It builds where I tell it to. However from 0:33 till 0:54 it show how it becomes inaccurate when I scroll. Then from 0:55 to 1:06 its accurate again and from 1:07 till the end of the video it show how it becomes inaccurate again.
Sorry for my bad english.
Here is my code.
class Tower
{
Texture2D Texture;
Vector2 Position;
SpriteFont Text;
bool Clicked = false;
public void LoadContent(ContentManager Content)
{
Texture = Content.Load<Texture2D>("Tiles/Towers/Tower1");
Text = Content.Load<SpriteFont>("Fonts/Font");
}
/// <Add tower to the mouse position summary>
/// In this update method when the mouse is clicked, We will calculate the mouse position in the game world and then set it to the tower.
/// We divide by 32 and then multiple by 32 so we can move the tower every 32px instead of every one pixel
/// </Add tower to the mouse position summary>
public void Update()
{
//ClickOnTower();
if (MouseCursor.LastMouseState.LeftButton == ButtonState.Released && MouseCursor.CurrentMouseState.LeftButton == ButtonState.Pressed)
{
Position.X = (int)(MouseCursor.Position.X -ScreenScrolling.ScreenOffset.X) / 32;
Position.Y = (int)(MouseCursor.Position.Y -ScreenScrolling.ScreenOffset.Y) / 32;
Position.X = Position.X * 32;
Position.Y = Position.Y * 32;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position + ScreenScrolling.ScreenOffset, Color.White);
//spriteBatch.DrawString(Text, " " + MouseCursor.Position, new Vector2(100, 150), Color.Black);
}
class TowerManager
{
List<Tower> Towers = new List<Tower>();
void AddTower()
{
}
public void Update(ContentManager Content)
{
if (MouseCursor.LastMouseState.LeftButton == ButtonState.Released && MouseCursor.CurrentMouseState.LeftButton == ButtonState.Pressed)
{
Towers.Add(new Tower());
for (int i = 0; i < Towers.Count; i++)
Towers.LoadContent(Content);
for (int i = Towers.Count - 1; i < Towers.Count; i++)
Towers.Update();
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (int i = 0; i < Towers.Count; i++)
Towers.Draw(spriteBatch);
}
}
class ScreenScrolling
{
Texture2D Texture;
public static Vector2 ScreenOffset;
Rectangle right, left, up, down;
public ScreenScrolling(GraphicsDeviceManager graphics)
{
right = new Rectangle(0, 0, 25, graphics.PreferredBackBufferHeight); //Right red line
left = new Rectangle(graphics.PreferredBackBufferWidth - 25, 0, 25, graphics.PreferredBackBufferHeight); // Left Red Line
up = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, 25); //Up red Line
down = new Rectangle(0, graphics.PreferredBackBufferHeight - 25, graphics.PreferredBackBufferWidth, 25); // Down Red Line
}
public void LoadContent(ContentManager Content)
{
Texture = Content.Load<Texture2D>("Side");
}
public void Update()
{
if (MouseCursor.MouseCursoreRec.Intersects(right))
{
ScreenOffset.X += 5f;
}
if (MouseCursor.MouseCursoreRec.Intersects(left))
{
ScreenOffset.X -= 5f;
}
if (MouseCursor.MouseCursoreRec.Intersects(up))
{
ScreenOffset.Y += 5f;
}
if (MouseCursor.MouseCursoreRec.Intersects(down))
{
ScreenOffset.Y -= 5f;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, right, Color.White);
spriteBatch.Draw(Texture, left, Color.White);
spriteBatch.Draw(Texture, up, Color.White);
spriteBatch.Draw(Texture, down, Color.White);
}
}