1

我正在尝试在 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 类链接到表单上的实际标签(也称为 BouncyBall)。

谁能帮我让我的球动起来?我开始没有想法了。谢谢!

4

0 回答 0