Results 1 to 7 of 7

Thread: make a picture bounce

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    1
    im new at this vb thing and i just want to know how to make an Object(picture1) bounce inside your screen (you know like the bouncing ball screen saver) upon loading the form

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    the best way is to put it inside another picture box and let it bounce around that (there's a problem with borders otherwise) put picture1 inside a second picture box (picture2) then add a timer to your form and add this code

    Code:
    Const X_CHANGE = 10
    Const Y_CHANGE = 10
    
    Dim lngWidth As Long
    Dim lngHeight As Long
    
    
    
    Private Sub Form_Resize()
    lngWidth = Picture2.Width - Picture1.Width
    lngHeight = Picture2.Height - Picture1.Height
    End Sub
    
    Private Sub Picture1_Resize()
    lngWidth = Picture2.ScaleWidth - Picture1.ScaleWidth
    lngHeight = Picture2.ScaleHeight - Picture1.ScaleHeight
    End Sub
    
    
    Private Sub Timer1_Timer()
    
    Static X As Long
    Static Y As Long
    
    'change x and y coords
    X = X + X_CHANGE
    Y = Y + Y_CHANGE
    
    If X > 2 * lngWidth Then X = X - (2 * lngWidth)
    If Y > 2 * lngHeight Then Y = Y - (2 * lngHeight)
    
    Picture1.Left = Abs(X - lngWidth)
    Picture1.Top = Abs(Y - lngHeight)
    
    
    End Sub
    my timer interval was set to 10, X_CHANGE and Y_CHANGE are how much the position of the picture box changes every timer interval, change the ratio of these to change the angle the picture box goes at

  3. #3
    Guest
    If you don't want to use a Timer Control, use the built in Timer function. Make a form with a CommandButton and a PictureBox

    Put this code in a module.

    Code:
    Function Jump(VarVal As Control)
        
        tmpTop = VarVal.Top
        
        PauseTime = 0.2
        Start = Timer
        Do While Timer < Start + PauseTime
            VarVal.Top = VarVal.Top - 1
        Loop
        Finish = Timer
        
        Pausetimer = 0.2
        Start = Timer
        Do While Timer < Start + PauseTime
            VarVal.Top = VarVal.Top + 1
        Loop
        Finish = Timer
        
        Pausetimer = 0.1
        Start = Timer
        Do While Timer < Start + PauseTime
            VarVal.Top = tmpTop
        Loop
        Finish = Timer
        
    End Function
    Now put this code in a CommandButton

    Code:
    Private Sub Command1_Click()
    
    A = 0
    Do While A < 1
    DoEvents
    RetVal = Jump(Form1.Picture1)
    Loop
    
    End Sub

  4. #4
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    why wouldn't you want to use a timer?

  5. #5
    Guest
    Some people, including myself, like to use as little controls as possible.

  6. #6
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Some people, including myself, like to use as little native vb commands as possible .

    Use GetTickCount instead of Timer()...

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb

    I prefer to use the QueryPerformanceFrequency and QueryPerformanceCounter API, beacuse it much more accurate than GetTickCount API.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width