I just want to , for example, move a ball or an object in a pictureBox, randomly, and make it rebound on the "wall" of the picturebox.
What api function should I use ? .. how?
:confused:
Printable View
I just want to , for example, move a ball or an object in a pictureBox, randomly, and make it rebound on the "wall" of the picturebox.
What api function should I use ? .. how?
:confused:
There are times when you should use the API, and times you shouldn't. It's a toolbox of sorts, and is kind of like having an automatic screwdriver. By this I mean, you can do without it just fine, but usually for larger projects, or if you don't have a large amount of time to research the several methods to accomplish a simple function (- that the API |CAN| do), it works fine.
Using that poor analogy of mine, I would suggest learning how to use the screwdriver manually first. To make a ball move around a form, if you use a picturebox, try using a timer. Timer's repeat code according to an interval. Set this interval, and thats how many time your code will repeat.
The trick is, x=X+int, y =Y+int (int is either +1 for positive motion, -1 for negative -- yeah i think that should work), this will move your ball in a perfectly diagonal motion. Now to keep it on the screen, make sure you have a loopback statement, i.e
If X = 630 (assuming the form is 640 pixels wide and the picture box is 10 (remember x and y coords are taken from the top left of the box if i am not mistaken) then int = -1 (now the ball will start going backwards), now you need one for the 0 side, so If X = 0 then int = +1, now the ball starts going the other way!
The y coordinate is the same idea. Now remember this will only make a ball bounce from corner to corner, to get it randomly bouncing, you will have to vary the int value, so that your rate of ascent and descent (movement) isn't perfectly diagonal,
hope this helped somewhat!
csnow
thanks for the tip!
I'll try something with this
There a VB demo that does this, as well as some other "Timer" demos. The believe the project name is called "Blanker"
Use the "ScaleWidth" and "ScaleHeight" properties of the picturebox. They measure only the inside of the picture box, not including the border.
Z.