我试图每秒调用一次方法20次,将A点推离B点。 该方法应使用以下变量:
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
答案 0 :(得分: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