Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

How to bounce an object in a form?

Last post 05-10-2008 12:34 AM by Veritant. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 03-17-2008 11:17 AM

    • corvin
    • Not Ranked
    • Joined on 02-16-2008
    • Philippines
    • New Member
    • Points 110

    How to bounce an object in a form?

     How to make a bouncing object from a form?  Any object must bounce from the edges of a form

    • Post Points: 10
  • 05-10-2008 12:34 AM In reply to

    • Veritant
    • Top 50 Contributor
    • Joined on 11-02-2001
    • United Kingdom
    • Guru
    • Points 1,930

    Re: How to bounce an object in a form?

    Well, I'n not sure I understand your question. But anyway, here is a fun bouncing object.

    Create a form with a small picturebox and a timer. Give the picturebox a different colour from the form by setting the background colour. Set the timer interval to 1, but do not enable it.

    Paste in the code below. Then run the program.

    Now when you click on the picturebox and give it a flick with the mouse and let go at the same moment, you can flick the picturebox around the form. It bounces around inside the form gradually slowing to a stop. Then give it another flick.

    Quite fun seeing it buzz around the form

    Code is in VB2008, you will have to adapt it for vb6 - have fun! :¬D

    Public Class Form1
    
        Private bMouseIsDown As Boolean = False
        Private oMEA As MouseEventArgs
        Private nMovestartTime As Long
        Private nVDistanceMovePerTick As Double
        Private nHDistanceMovePerTick As Double
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            bMouseIsDown = True
            oMEA = e
            nMovestartTime = My.Computer.Clock.TickCount
    
        End Sub
    
        Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
            If Not bMouseIsDown Then Exit Sub
            Dim iticks As Long
            iticks = My.Computer.Clock.TickCount - nMovestartTime
            nVDistanceMovePerTick = CDbl(e.Y - oMEA.Y) / CDbl(iticks)
            nHDistanceMovePerTick = CDbl(e.X - oMEA.X) / CDbl(iticks)
            Debug.Print("H=" & nHDistanceMovePerTick)
            Debug.Print("V=" & nVDistanceMovePerTick)
            bMouseIsDown = False
            nMovestartTime = My.Computer.Clock.TickCount
            Timer1.Enabled = True
        End Sub
    
        
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim iticks As Long
            iticks = My.Computer.Clock.TickCount - nMovestartTime
            Dim vMove As Long, hMove As Long
    
            vMove = nVDistanceMovePerTick * iticks
            hMove = nHDistanceMovePerTick * iticks
            nVDistanceMovePerTick = nVDistanceMovePerTick * 0.95
            nHDistanceMovePerTick = nHDistanceMovePerTick * 0.95
            Me.PictureBox1.Top = Me.PictureBox1.Top + vMove
            Me.PictureBox1.Left = Me.PictureBox1.Left + hMove
            If nVDistanceMovePerTick + nHDistanceMovePerTick = 0 Then
                Timer1.Enabled = False
            Else
                If Me.PictureBox1.Top < 0 Or Me.PictureBox1.Top > Me.Height - Me.PictureBox1.Height Then
                    Me.nVDistanceMovePerTick = -Me.nVDistanceMovePerTick
                End If
                If Me.PictureBox1.Left < 0 Or Me.PictureBox1.Left > Me.Width - Me.PictureBox1.Width Then
                    Me.nHDistanceMovePerTick = -Me.nHDistanceMovePerTick
    
                End If
            End If
    
        End Sub
    End Class
    
    
    Veritant (John)
    • Post Points: 5
Page 1 of 1 (2 items)