PictureBox反复左右移动

时间:2015-10-06 19:36:58

标签: vb.net visual-studio-2015

尝试使用此代码,以便图片框自动从左向右移动。代码没有给出任何错误,但是当我运行应用程序时,picturebox1只是保持静止并且什么都不做。

我在想:Timer1属性可能,没有弄乱timer1属性中的任何东西,不确定我是否应该,或者我可能需要添加更多代码或修复它;不确定。

(如果你很好奇我正在做什么以防万一,我有一个'敌人'从左到右来回左右移动等等,'玩家'必须得到经过它而没有击中它。)

Private Sub Timer1_Timer()
    Do
        PictureBox1.Left = PictureBox1.Left - 5
        If PictureBox1.Left <= 5 Then
            Do
                PictureBox1.Left = PictureBox1.Right + 5
            Loop Until PictureBox1.Left >= 1000
        End If
    Loop
End Sub

1 个答案:

答案 0 :(得分:0)

如同@ user3697824所述,您发布的Sub未连接到表单上的Timer。通常在使用计时器时,您可以将要运行的代码放在Timer的Tick方法中。

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'Your code goes here.
End Sub

您可以通过在Sub的签名末尾添加Handles Timer1.Tick语句来修复上面的代码,如下所示:

Private Sub Timer1_Timer() Handles Timer1.Tick
    Do
        PictureBox1.Left = PictureBox1.Left - 5
        If PictureBox1.Left <= 5 Then
            Do
                PictureBox1.Left = PictureBox1.Right + 5
            Loop Until PictureBox1.Left >= 1000
        End If
    Loop
End Sub