1

我试图每秒调用一个方法 20 次以将 A 点推离 B 点。该方法应使用以下变量:

  • A点(X)
  • A点(Y)
  • B点(X)
  • B点(Y)
  • 力量

A 点和 B 点越靠近,它们彼此远离的速度就越快。强度变量控制每个刻度的距离以及不再推动点 A 的点之间的距离......

基本上我试图慢慢地将光标推离指定点。

有什么想法可以实现吗?

草图

这是我的尝试,不幸的是,如果距离增加,光标移动得更快......

    'PosX and PosY are the percentage of the screen width/height

    'Calculate the real position
    Dim ScreenPosX As Integer = Screen.PrimaryScreen.WorkingArea.Width * (PosX / 100)
    Dim ScreenPosY As Integer = Screen.PrimaryScreen.WorkingArea.Height * (PosY / 100)

    Dim PointOffsetX As Integer = Cursor.Position.X - ScreenPosX
    Dim PointOffsetY As Integer = Cursor.Position.Y - ScreenPosY

    If PointOffsetX > -Strength And PointOffsetX < Strength Then
        Dim StrengthFactorX As Integer = Strength - ScreenOffsetX
        Dim StrengthFactorY As Integer = Strength - ScreenOffsetY

        Cursor.Position = New Point(Cursor.Position.X + StrengthFactorX, Cursor.Position.Y + StrengthFactorY)
        'Would be the same for Y. Obviously doesn't work though.
    End If
4

1 回答 1

1

这段代码对我有用。我使用了一个按钮和计时器 ontick 事件。您可以调整常数。

Dim currentObjectAccelerationX As Decimal = 0
Dim currentObjectAccelerationY As Decimal = 0

Dim currentObjectPointX As Decimal
Dim currentObjectPointY As Decimal

Const maxDistance As Integer = 50
Const accelerationDenominator As Integer = 50
Const deceleration As Decimal = 0.2

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    currentObjectPointX = Button1.Location.X
    currentObjectPointY = Button1.Location.Y

    Timer1.Start()
End Sub

Private Sub onTick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    Dim MousePointX As Integer = Me.PointToClient(Cursor.Position).X
    Dim MousePointY As Integer = Me.PointToClient(Cursor.Position).Y

    Dim OffsetX As Decimal = currentObjectPointX - MousePointX
    Dim OffsetY As Decimal = currentObjectPointY - MousePointY



    If Math.Abs(OffsetX) < maxDistance AndAlso Math.Abs(OffsetY) < maxDistance Then


        Dim accelerationX As Decimal = 0
        Dim accelerationY As Decimal = 0

        If OffsetX > 0 Then
            accelerationX = 1 + (maxDistance - OffsetX) / accelerationDenominator
        Else
            accelerationX = -1 + (-maxDistance - OffsetX) / accelerationDenominator
        End If

        If OffsetY > 0 Then
            accelerationY = 1 + (maxDistance - OffsetY) / accelerationDenominator
        Else
            accelerationY = -1 + (-maxDistance - OffsetY) / accelerationDenominator
        End If


        currentObjectAccelerationX += accelerationX
        currentObjectAccelerationY += accelerationY
    End If

    currentObjectPointX += currentObjectAccelerationX
    currentObjectPointY += currentObjectAccelerationY

    If (Math.Abs(currentObjectAccelerationX) <= deceleration) Then
        currentObjectAccelerationX = 0
    ElseIf (currentObjectAccelerationX > deceleration) Then
        currentObjectAccelerationX -= deceleration
    ElseIf (currentObjectAccelerationX < deceleration) Then
        currentObjectAccelerationX += deceleration
    End If

    If (Math.Abs(currentObjectAccelerationY) <= deceleration) Then
        currentObjectAccelerationY = 0
    ElseIf (currentObjectAccelerationY > deceleration) Then
        currentObjectAccelerationY -= deceleration
    ElseIf (currentObjectAccelerationY < deceleration) Then
        currentObjectAccelerationY += deceleration
    End If

    Button1.Location = New Point(currentObjectPointX, currentObjectPointY)
End Sub
于 2013-10-21T06:04:26.987 回答