为什么我的游戏解释鼠标指针坐标错误

时间:2014-01-28 23:27:57

标签: c# xna monogame

我正在制作一个单一游戏(2D)游戏,当我尝试获得鼠标坐标时,他们错了。我不知道问题是什么,但这里是我得到坐标的代码:

MouseState mouseState;
mouseState = Mouse.GetState();
test = new Tower(TowerTexture, new Vector2(mouseState.X, mouseState.Y));
//test is drawn where mouse pointer is thought to be and it is drawn off

这是塔绘图代码:

    foreach (Tower tower in towers)
    {
        tower.Draw(spriteBatch);
    }

以下是塔的绘图功能:

public virtual void Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(texture, center, null, Color.White, rotation,
    origin, 1.0f, SpriteEffects.None, 0);
}

还有一件事是,作为鼠标指针,真实的一个更接近,左上角假定鼠标坐标的偏移量较小,但随着你越来越接近屏幕的右下角,假设鼠标坐标更远。 老实说,我不知道出了什么问题,但是对于可能出现的问题的任何想法都会受到赞赏。谢谢!

2 个答案:

答案 0 :(得分:1)

这是对这个问题和question on hold的回应。要解决此问题,您可以在绘制图像时缩小图像。我不太确定center的值是多少,但我的猜测是它是一个矩形,其中心位于鼠标指针处。要缩小图像,请尝试以下方法:

Rectangle center;

public Tower(Texture2D TowerTexture, Vector2 location)
{
    float scaledown = 10;
    float XOffset = TowerTexture.Width / (2 * scaledown); //get an X and Y offset to center the image in the rectangle
    float YOffset = TowerTexture.Height / (2 * scaledown);
    this.center = new Rectangle(location.X + XOffset, location.Y + YOffset, 
        XOffset * 2, YOffset * 2);
}

然后使用center作为目标矩形,像以前一样绘制此图像。我在没有编译器或调试的情况下编写了这段代码,但我认为它应该给你一个基本的想法。 HTH

答案 1 :(得分:0)

错误的是我的图像分辨率大于我的屏幕分辨率。