Solved

# Modify code to make floating Pictue stay within form boundries

Posted on 2012-08-29
967 Views
How can i modify this code so picture does not disappear off the form
Sub MovePic()
Dim x As Single, y As Single, velocity As Single, angle As Single, vangle As Single
CenterPic

Me.picJackpot.ZOrder 0
velocity = 2
angle = Rnd * 6.283
x = picJackpot.Left: y = picJackpot.Top

Do
If Rnd < 0.1 Then vangle = (Rnd - 0.5) * 0.5 'Change direction velocity randomly 10%chance
angle = angle + (vangle + 6.283) Mod 6.283 'change direction
x = x + Cos(angle) * velocity 'move position
y = y + Sin(angle) * velocity
picJackpot.Move x, y 'move image
Loop While mStopJackPot = False 'Forms.Count Or yourchoise
End Sub
0
Question by:isnoend2001

LVL 44

Accepted Solution

``````Sub MovePic()
Dim x As Single, y As Single, velocity As Single, angle As Single, vangle As Single
CenterPic

Me.picJackpot.ZOrder 0
velocity = 2
angle = Rnd * 6.283
x = picJackpot.Left: y = picJackpot.Top

Do
If Rnd < 0.1 Then vangle = (Rnd - 0.5) * 0.5 'Change direction velocity randomly 10%chance
angle = angle + (vangle + 6.283) Mod 6.283 'change direction
x = x + Cos(angle) * velocity 'move position
y = y + Sin(angle) * velocity
If x < 0 Then x = 0
If x + picJackpot.Width > ScaleWidth Then x = ScaleWidth - picJackpot.Width
If y < 0 Then y = 0
If y + picJackpot.Height > ScaleHeight Then y = ScaleHeight - picJackpot.Height
picJackpot.Move x, y 'move image
Loop While mStopJackPot = False 'Forms.Count Or yourchoise
End Sub
``````
0

Author Closing Comment

Thank you
0

LVL 44

Expert Comment

You're welcome and I'm glad I was able to help.

My profile contains links to some articles I've written that may interest you.
Marty - MVP 2009 to 2012
0

## Featured Post

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…