2017-05-29 52 views
-1

我正在做一个游戏,你点击射击朝光标过得弹射击弹丸。但是,我不知道如何去做使弹丸向光标方向飞行。我试图找到像这样的斜线...使用光标

public bool PreFilterMessage(ref Message m) 
     { 
      if(m.Msg == 0x0201) 
      { 
       Point MousePos = PointToClient(Form.MousePosition); 

       slope = (MousePos.Y - aCharacter.Location.Y)/(MousePos.X - aCharacter.Location.X); 
       aLaser.Location = aCharacter.Location; 
       if (MousePos.X < aCharacter.Location.X) 
        lasDir = -1; 
       else 
        lasDir = 1; 
       laserLaunched = true; 
       return true; 
      } 

    private void aChMvmTimer_Tick(object sender, EventArgs e) 
    { 

     if(laserLaunched) 
     { 
      aLaser.Location = new Point(aLaser.Location.X + lasDir, aLaser.Location.Y + (slope * lasDir)); 
      aLaser.Visible = true; 

     } 

     else 
     { 
      aLaser.Visible = false; 
     } 
} 

但是,它没有在光标处拍摄,并且在拍摄的地方几乎是随机的。我会如何去做这件事?

我尝试另一种解决办法,但我怀疑,我做的对。

int currentTime = Environment.TickCount; 
     double deltaTime = (currentTime - lastTime)/1000.0; 
     lastTime = currentTime; 
     if (laserLaunched) 
     { 

      int movementSpeed = 20; 
      //aLaser.Location = new Point(aLaser.Location.X + lasDir, aLaser.Location.Y + (slope * lasDir)); 
      aLaser.Visible = true; 
      double x = aLaser.Location.X - curMousePos.X; 
      double y = aLaser.Location.Y - curMousePos.Y; 
      double loc = ((int)x^2) + ((int)y^2); 
      loc = Math.Sqrt(((int)x^2) + ((int)y^2)); 
      x = (x/(int)loc); 
      y = (y/(int)loc); 
      x *= (int)deltaTime * movementSpeed; 
      y *= (int)deltaTime * movementSpeed; 
      aLaser.Location = new Point(aLaser.Location.X + (int)x, aLaser.Location.Y + (int)y); 
     } 

我做对了吗?

+0

“它不工作” 是没有问题的描述。而“最细微”的限定词根本没有改善。 –

+0

感谢您指出了这一点!当我发布时我没有意识到。 – Beldar4000

回答

0

最简单的方法是使用简单的矢量运算:

把那从A点到B的向量:

V = B - A // That means subtracting each component of B from the respective component of A. 

如果您正在使用System.Windows.Point替代的WinForms版本,你可以使用Point.Subtract(Point,Point)方法可以立即为您提供一个Vector对象。

然后正常化是矢量,这样你就可以更好地为弹丸的希望的移动速度。归一化的向量仍具有在各部件相同的相对大小,但为1的总长度要做到这一点,可以通过该矢量的总长度除以每个组件或如果你使用前面提到的方法System.Windows使用Vector.Normalize()

一旦你的归一化向量,就可以通过矢量量,乘以所需的速度和你的Tick事件的帧率*改变每个替您的实际弹丸位置的坐标。

同样,你可以做到这一点“手动”或使用Vector内置功能:

newLocation = Vector.Add(Vector.Multiply(movementVector, movementSpeed * deltaTime), currentLocation); 

*我在这里使用可以通过存储系统时将予收购之deltaTime(以毫秒),每次调用tick函数并将当前系统时间与前一时间进行比较。将它除以1000,您将得到一个浮点值,以秒为单位告诉您时间差异,然后您可以用“像素/秒”来指定抛射体移动速度。这使得独立于您的蜱功能在运行帧率的实际运动,所以它不容易成为,如果玩家的CPU是一个瓶颈慢,也不会改变,如果你决定改变你的滴答更新的频率,带来更顺畅后来的游戏体验。

例子:

private int lastTime; // Initialize this to Environment.TickCount in your constructor. 

private void TickUpdate() { // Called by your tick event. 

    int currentTime = Environment.TickCount; 
    double deltaTime = (currentTime - lastTime)/1000.0; 
    lastTime = currentTime; 

    // Do stuff with deltaTime. 
} 
+0

这是有道理的,但我没有使用WPF。我怎样才能做到这一点呢? – Beldar4000

+0

那么,您仍然可以在这些位置明确指向Windows.Point,并在Drawing.Point中使用最终结果的坐标。或者你正如我在帖子中指出的那样,亲手做所有的事情。这意味着您只需直接对X个Y组件执行所有操作,而不是使用便捷方法。所以你拿'B.X - A.X','B.Y - A.Y',计算得到的向量的长度('Sqrt(X^2 + Y^2)')并且除以X和Y.然后,将X和Y乘以移动速度和deltaTime,最后将其添加到旧位置的相应X和Y. –

+0

这应该可行,但我对deltaTime的使用方法有点好奇。你能深入了解如何获得它吗?谢谢! – Beldar4000