1

I'm using Visual Studio 2010 Professional.

I have one form (and its associated vb file) and another, separate vb file. When I go to compile and debug my code, my building succeeds, and the form displays, but the "ball" doesn't move.

My startup class:

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

My other, separate 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

My form displays, but my "ball" doesn't move. What I would like is for the variables and subroutines in my Ball class to control the movement of my Label "ball". Any help, suggestions?

4

1 に答える 1

0

おそらく、While Trueループの代わりにタイマーを使用する必要があります。ループは、While TrueGUI に画面を更新する機会を与えていません。

がコントロールであると仮定するBallと、フォームのコレクションに追加する必要があります。

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

あなたのBallクラスが何をしているかは不明です。内部変数を更新しているだけで、実際にはコントロールを動かしていないように見えます。これは、あなたが達成しようとしていると私が疑っていることです。

于 2012-09-26T15:12:56.417 に答える