|
-
May 29th, 2001, 08:31 AM
#1
Thread Starter
Member
NewBie Question
how can i make a picture bounce aroound but keep it with in a certain area
What is Life? One big dream or one Big nightmere.
-
May 30th, 2001, 07:10 PM
#2
Addicted Member
More info
Try reposting...provide a bit more info...like what you mean by bouncing. Real physics? just up and down? degrading up and down? side to side?...etc...Best of luck
Cbomb
Techie 
-
May 31st, 2001, 06:12 AM
#3
New Member
Add a timer and a picture box to a form the paste this in and run:
'***
Dim AccelX As Single, AccelY As Single
Dim PosX As Single, PosY As Single
Const Gravity = 6 'How strong the gravity is
Const Damping = 0.9 'How much bouncing damps the acceleration
Private Sub Form_Load()
Randomize Timer
'Set the defaults for the 'ball'
PosX = (MaxX - Picture1.Width) \ 2
PosY = 0
AccelX = (Rnd * 20) - 10
'Start the animation timer
With Timer1
.Interval = 50
.Enabled = True
End With
End Sub
Private Sub Timer1_Timer()
Dim MaxX As Long, MaxY As Long
'Get the size of the bouncing area
MaxX = Form1.Width - 120 - Picture1.Width
MaxY = Form1.Height - 400 - Picture1.Height
'Increce vertical acceleration due to gravity
AccelY = AccelY + Gravity
'Move the image based on it's acceleration
PosX = PosX + AccelX
PosY = PosY + AccelY
'Check for collisions
If PosX < 0 Then
PosX = -PosX
AccelX = -AccelX * Damping
ElseIf PosX > MaxX Then
PosX = MaxX
AccelX = -AccelX * Damping
End If
If PosY < 0 Then
PosY = -PosY
AccelY = -AccelY * Damping
ElseIf PosY > MaxY Then
PosY = MaxY
AccelY = -AccelY * Damping
End If
'Finally move picture to new location
Picture1.Move PosX, PosY
End Sub
'***
Hope this helps,
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|