Visual Basic Resetting a Timer

Klar Shultz 1 Reputation point
2021-11-08T23:08:39.693+00:00

I want to use a timer to animate a picture box throwing something, like a character attacking. But it is only doing it once. Im not if i need to reset the timer.

Private Sub tmrAttack1_Tick(sender As Object, e As EventArgs) Handles tmrAttack1.Tick

        Dim player1 As Integer = picPlayer1.Location.X

        player1 = -1

        picPlayer1Attack.Location = New Point(player1, picPlayer1.Location.Y)


        If picPlayer1Attack.Bounds.IntersectsWith(lblLeftBarrier.Bounds) Then
            tmrAttack1.Stop()
            picPlayer1Attack.Visible = False

        End If

        If picPlayer1Attack.Bounds.IntersectsWith(lblRightBarrier.Bounds) Then
            tmrAttack1.Stop()
            picPlayer1Attack.Visible = False

        End If

    End Sub
End Class
Developer technologies VB
{count} votes

1 answer

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-11-09T05:49:18.053+00:00

    Hi @Klar Shultz ,
    To reset Timer, try:

        Private Sub tmrAttack1_Tick(sender As Object, e As EventArgs) Handles tmrAttack1.Tick  
            Dim player1 As Integer = picPlayer1.Location.X  
            player1 = -1  
            picPlayer1Attack.Location = New Point(player1, picPlayer1.Location.Y)  
            If picPlayer1Attack.Bounds.IntersectsWith(lblLeftBarrier.Bounds) Then  
                tmrAttack1.Stop()  
                picPlayer1Attack.Visible = False  
      
                tmrAttack1.Start()  
            End If  
            If picPlayer1Attack.Bounds.IntersectsWith(lblRightBarrier.Bounds) Then  
                tmrAttack1.Stop()  
                picPlayer1Attack.Visible = False  
      
                tmrAttack1.Start()  
            End If  
      
        End Sub  
    

    Timer.Stop() , then Timer.Start() worked as a reset.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.