0

I am creating a game in which I use a function:

int x()
{
    int px = 100;
    int pxend = 128;
    int xx = 0;
    for (int i = 0; i < 6; i++)
    {
        l = Mouse.GetState().X;
        if (l > px && l < pxend)
        {
            xx = px;
            break;
        }
        else
        {
            px += 128;
            pxend += 128;
        }
    }
    return xx;
}

int y()
{
    int px = 100;
    int pxend = 128;
    int xx = 0;
    for (int i = 0; i < 4; i++)
    {
        l = Mouse.GetState().X;
        if (l > px && l < pxend)
        {
            xx = px;
            break;
        }
        else
        {
            px += 128;
            pxend += 128;
        }
    }
    return xx;
}

But when I debug I get a l value as negative even though the debug is initiate after a click on the GameWindow screen. Sometimes it works fine, sometimes does not. I don't know why it gives me negative value. And when I use them with my Rectangle I get Rectangle.X and Rectangle.Y as 0.

4

1 回答 1

0

来自MSDN

如果鼠标位于客户区的左侧(游戏窗口的内部,不包括边框),则返回的坐标为负数。如果鼠标在客户区的右侧,则返回的坐标大于窗口的客户区宽度。

这更像是一个评论而不是一个答案(我没有足够的声誉来评论),但我只看到当鼠标在窗口外时才会发生这种情况......我建议将该GetState()调用从 for循环并在循环外调用它一次 - 这是唯一让我印象深刻的事情。您说有时有效,有时无效,这是完全随机的还是您第一次GetState()拨打电话时?

于 2013-09-22T17:58:32.487 回答