Looking for VB function to bounce graphic around screen - Please
Author Message
Looking for VB function to bounce graphic around screen - Please

Is there a formula or book somewhere that might explain how to bounce an
object around the screen where, when it hits the corners or edges it will
bounce back. I have figured out all my own drawing functions I needed by this
one still puzzles me.

Thanks for you help

Michael Gernaey

Mon, 03 Nov 1997 03:00:00 GMT
Looking for VB function to bounce graphic around screen - Please

Quote:
> Is there a formula or book somewhere that might explain how to bounce an
> object around the screen where, when it hits the corners or edges it will
> bounce back. I have figured out all my own drawing functions I needed by this
> one still puzzles me.

Sub Timer1()

const AmountToMove = 50
Static MoveRight as integer  'boolean
Dim WhichWay as integer

If MoveRight THen
'check to see if hit right side of screen.
If object1.Left + object1.width >= Screen.Width  Then
WhichWay = -1  'move left
MoveRight = False
Else
WhichWay = 1
MoveRight = True
End If
Else    'it is moving left
'check if hit left side of screen
If object1.Left <= 0 Then
WhichWay = 1  'positive to the right now
MoveRight = True
Else
WhichWay = -1
MoveRight = False
End If
End If

'move the object
object1.Left = object1.Left - (WhichWay * AmountToMove)

This is off the top of my head, you'll have to test it out.  The best
approach would be to put it into a procedure, and pass the object you
are moving(assuming it is a control of some kind) to the procedure.

Sub MoveX (Source as Control)   'souce is a picture or image box
'above code
Similar for movement in Y direction, just remember the top of the
screen is 0, and you increase Y as you go down the screen.  This
always screws me up for a bit.
Goodluck.
----------------------------------------------------------------------
Greg Carter
Fourth Year Electrical Engineering
Carleton University

World Wide Web: http://chat.carleton.ca/~gcarter
----------------------------------------------------------------------

Wed, 05 Nov 1997 03:00:00 GMT
Looking for VB function to bounce graphic around screen - Please

Quote:

> > Is there a formula or book somewhere that might explain how to bounce an
> > object around the screen where, when it hits the corners or edges it will
> > bounce back. I have figured out all my own drawing functions I needed by this
> > one still puzzles me.

[Code to move object delected]

Quote:
> 'move the object
> object1.Left = object1.Left - (WhichWay * AmountToMove)

Looking over this it should read

object1.Left = object1.Left + (WhichWay * AmountToMove)

Michael,  Did this code work?
----------------------------------------------------------------------
Greg Carter
Fourth Year Electrical Engineering
Carleton University

World Wide Web: http://chat.carleton.ca/~gcarter
----------------------------------------------------------------------

Thu, 06 Nov 1997 03:00:00 GMT
Looking for VB function to bounce graphic around screen - Please

Quote:

>Subject: Re: Looking for VB function to bounce graphic around screen - Please
>Date: Sun, 21 May 1995 20:15:55 GMT

>> > Is there a formula or book somewhere that might explain how to bounce an
>> > object around the screen where, when it hits the corners or edges it will
>> > bounce back. I have figured out all my own drawing functions I needed by
>this
>> > one still puzzles me.
>[Code to move object delected]
>> 'move the object
>> object1.Left = object1.Left - (WhichWay * AmountToMove)
>Looking over this it should read
>  object1.Left = object1.Left + (WhichWay * AmountToMove)
>Michael,  Did this code work?
>----------------------------------------------------------------------
>Greg Carter
>Fourth Year Electrical Engineering
>Carleton University

>World Wide Web: http://chat.carleton.ca/~gcarter
>----------------------------------------------------------------------

Well the only problem is that when I reach a point where going any farther
there needed to be a check on which variable would become ( - ) and have it
start to move in the opposite direction. With the help of many people I have
figured it out. Actually although I appreciate the great help by everyone who
answered because many did with the right answer, I found it (because of
someone else's suggestion) in the SAMPLE VB program Blanker..

Thanks to everyone who answered, my project is finished and I owe it to all
you VB friends out there :)

Michael Gernaey

Thu, 06 Nov 1997 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages