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
Printable View
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
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
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 atCode:
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
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.
Now put this code in a CommandButtonCode: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
Code:Private Sub Command1_Click()
A = 0
Do While A < 1
DoEvents
RetVal = Jump(Form1.Picture1)
Loop
End Sub
why wouldn't you want to use a timer?
Some people, including myself, like to use as little controls as possible.
Some people, including myself, like to use as little native vb commands as possible ;).
Use GetTickCount instead of Timer()...
I prefer to use the QueryPerformanceFrequency and QueryPerformanceCounter API, beacuse it much more accurate than GetTickCount API.