为什么我的MouseState不正确?

时间:2014-01-18 05:15:57

标签: c# xna monogame

我正在尝试使用鼠标输入将塔放置在我正在进行的塔防游戏中。一切正常,但是当我放下塔时,它不会放在指针所在的位置。我跑了一个小测试,让游戏画一个精灵,它认为我的指针是它,它把它画得远离我的指针。这是我用来获取鼠标坐标的代码:

mouseState = Mouse.GetState(); //mouseState is of type MouseState

cellX = (int)(mouseState.X / 80);
cellY = (int)(mouseState.Y / 80);

tileX = cellX * 80;
tileY = cellY * 80;

以下是塔使用合作伙伴的代码:

Tower tower = new Tower(BlueberryBushTexture, new Vector2(tileX, tileY));

我唯一能想到的就是我必须使用MouseState coords错误。谢谢你!!!

2 个答案:

答案 0 :(得分:0)

不是分割和乘法,而是简单地使用模数!

mouseState = Mouse.GetState();

tileX = (int)(mouseState.X - (mouseState.X % 80));
tileY = (int)(mouseState.Y - (mouseState.Y % 80));

/*
This way it will always be highest multiple of 80 lower the the Mouse's position

E.g. mouseState = (243, 712)
tileX = (int)(243 - (243 % 80));
      = (int)(243 - (3));
      = 240;

tileY = (int)(712 - (712 % 80));
      = (int)(712 - (72));
      = 640;

Doing check sum's like this can be really helpful when later modifying 
(Say you make the center of the tile its origin, instead of the top-left corner)
*/

这可以帮助你克服困难,并希望你未来的游戏项目。

莫纳。

答案 1 :(得分:0)

这是旧的,但我看到了...... 您除以80然后乘以80,您可以使用Math.Floor()。 这并没有给你一个瓦片索引它给你鼠标位置。你需要弄清楚这个点是什么,如果你有一个移动的摄像头,你还需要考虑相机的位置。

相关问题