跟踪鼠标移动和幅度

时间:2013-08-15 19:05:53

标签: c# xna

我正在制作一个玩家控制坦克的简单游戏。炮塔的旋转将由鼠标移动控制。代码目前看起来像这样:

if (Game.MouseState.Y < yMovementBorder)
   PossessedTurretPitchValue += dist;

if (Game.MouseState.Y > yMovementBorder)
   PossessedTurretPitchValue -= dist;

if (Game.MouseState.X < xMovementBorder)
   PossessedTurretYaw += rotationDist / 6;

if (Game.MouseState.X > xMovementBorder)
   PossessedTurretYaw -= rotationDist / 6;

xMovementBorder和yMovementBorder是表示游戏屏幕中点的值。问题是鼠标的任何移动都会导致炮塔转动直到其最大俯仰角/偏航角。我怎样才能使它能够读取鼠标移动及其大小(即:鼠标的轻微移动只会导致炮塔略微俯仰/偏转)?

2 个答案:

答案 0 :(得分:0)

在更新方法中,您应该存储对先前鼠标状态和当前鼠标状态的引用。然后使用这两个变量来计算自上次更新以来鼠标移动了多少。需要进行一些调整以使其恰到好处,但尝试这样的事情:

//define your private variables
private MouseState prevMouseState = null;
private MouseState currMouseState = Game.MouseState;

public void Update(GameTime gt)
{
    prevMouseState = currMouseState;
    currMouseState = Game.MouseState;

    //calculate how much the mouse has moved since the last update
    var dX = currMouseState.X - prevMouseState.X;
    var dY = currMouseState.Y - prevMouseState.Y;

    //do your rotating depending on the values of dX and dY
}

答案 1 :(得分:0)

我认为您要做的是根据鼠标距离中心的距离来设置限制。

像:

if (Game.MouseState.Y < yMovementBorder)
    if (PossessedTurretPitchValue < yMovementBorder*someFactor)
        PossessedTurretPitchValue += dist;

其他三个案件同样如此。

如果没有设置限制,与中心的任何偏差都会导致炮塔移动。它将一直在移动,并最终在某处移动。如果我理解正确的话,这就是你的代码现在所做的事情。