我正在尝试在Visual Basic 2010中编写一个弹跳球应用程序。我有两个类:一个用于构建球对象并移动球,我有一个球类,其中包含球的位置和速度。
我的主要课程:
Public Class Bouncer 'start of main class
Private ball As BouncyBall 'private ball object field
'form load event handler
Private Sub CST8333_Lab3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ball = New BouncyBall(Me) 'call ball constructor
End Sub 'end form load event handler
'timer event handler to control ball movement
Private Sub Timer_Tick(sender As System.Object, e As System.EventArgs) Handles Timer.Tick
ball.MoveBall() 'move the ball object
End Sub 'end timer event handler
End Class 'end main class
我的球类:
Class BouncyBall 'start of ball class
'private field variables
Private ballX As Integer
Private ballY As Integer
Private ballMovementX As Integer
Private ballMovementY As Integer
Private location As Point
Private _bouncer As Bouncer
'ball object constructor
Sub New(bouncer As Bouncer)
'instantiate variables
_bouncer = bouncer
ballX = 50
ballY = 50
ballMovementX = 5
ballMovementY = 5
location = New Point(ballX, ballY)
End Sub 'end ball object constructor
'subroutine to move ball
Public Sub MoveBall()
If (ballX >= _bouncer.Width) Then 'check if ball hits right side
ballMovementX = -ballMovementX
ElseIf (ballX <= 0) Then 'check if ball hits left side
ballMovementX = -ballMovementX
End If
If (ballY >= _bouncer.Height) Then 'check if ball hits bottom
ballMovementY = -ballMovementY
ElseIf (ballY <= 0) Then 'check if ball hits top
ballMovementY = -ballMovementY
End If
'move ball
ballX += ballMovementX
ballY += ballMovementY
Me.location = New Point(ballX, ballY)
End Sub 'end subroutine to move ball
End Class 'end ball class
球对象由围绕表单移动的标签组成。球对象被称为BouncyBall,我希望我的MoveBall()子程序中的规则引导球形式的球。 但是,我的球根本不动。我的计时器已启用,间隔为50毫秒,因此这不是我的问题。我认为我的问题是我的MoveBall()规则使用的变量实际上并没有更新我的标签的属性。
找到一种方法来使用我的变量来更新我的BouncyBall球的位置后,我的球仍然没有移动。我仍然认为我需要以某种方式将包含我的规则的BouncyBall类链接到表单上的实际Label(也称为BouncyBall)。
有人可以帮助我让球移动吗?我开始没有想法了。谢谢!