1

我正在使用 Visual Studio 2010 Professional。

我有一个表格(及其关联的 vb 文件)和另一个单独的 vb 文件。当我去编译和调试我的代码时,我的构建成功,并且表单显示,但是“球”没有移动。

我的创业班:

Public Class Bouncer

    Private bouncingBall As Ball

    Private Sub CST8333_Lab3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        bouncingBall = New Ball(Me)
        'Me.Controls.Add(Ball)
    End Sub

    Private Sub Timer_Tick(sender As System.Object, e As System.EventArgs) Handles Timer.Tick
        bouncingBall.MoveBall()
    End Sub
End Class

我的另一个单独的班级:

Public Class Ball

    Private ballX As Integer
    Private ballY As Integer
    Private ballMovementX As Integer
    Private ballMovementY As Integer
    Private _bouncer As Bouncer

    Sub New(bouncer As Bouncer)
        _bouncer = bouncer
        ballX = 50
        ballY = 50
        ballMovementX = 5
        ballMovementY = 5
    End Sub

    Public Function GetBallX() As Integer
        Return ballX
    End Function

    Public Sub MoveBall()
        If (ballX >= _bouncer.Width) Then
            ballMovementX = -ballMovementX
        ElseIf (ballX <= 0) Then
            ballMovementX = -ballMovementX
        End If
        If (ballY >= _bouncer.Height) Then
            ballMovementY = -ballMovementY
        ElseIf (ballY <= 0) Then
            ballMovementY = -ballMovementY
        End If
        ballX += ballMovementX
        ballY += ballMovementY
    End Sub
End Class

我的表格显示,但我的“球”没有移动。我想要的是我的 Ball 类中的变量和子例程来控制我的标签“球”的运动。任何帮助,建议?

4

1 回答 1

0

您可能应该使用计时器而不是While True循环。While True循环没有给 GUI 更新屏幕的机会。

假设Ball是一个控件,它需要被添加到表单的集合中:

bouncingBall = New Ball(Me)
Me.Controls.Add(bouncingBall)
bouncingBall.MoveBall()

目前尚不清楚您的Ball班级在做什么。看起来它只是更新内部变量而不是实际移动控件,这就是我怀疑你正在尝试完成的事情。

于 2012-09-26T15:12:56.417 回答