I am making a game in which a ship shoots with lasers on other objects.The laser and the target are both picture boxes.I want to know how can i know when one picture "touches" another i.e. when the target is hit.
Thanks!
Printable View
I am making a game in which a ship shoots with lasers on other objects.The laser and the target are both picture boxes.I want to know how can i know when one picture "touches" another i.e. when the target is hit.
Thanks!
use the intersectrect API function. no time to describe in detail, try msdn or api forums
Heres basicly what your need to do:
using the bottom, right, top and left properties of the pictureboxes, you can tell if an object is within the box.
To get the boxes right value use
to get the bottom value useCode:picbox.left+picbox.left
Then check to see if the 'laser' is within the actual picbox's values. Also I would recommend you use a image control instead of a picturebox. They take MUCH less memory and contain all the properties you'll need. Also define the values BEFORE you check if a hit so that your code executes faster. Hope this helps :)Code:picbox.top + picbox.height
nahh, intersectrect is better. faster, cleaner, never lies, easy to understand.
Yes intersect is mostly better(btw I typed mine at te same time as you so I didnt see yours), BUT he(she?) seemed to be a beginning programmer so learning how to do things like that is crucial.
Also, if he is newer, it would have probably helped to have given him the API declare, I have included both IntersectRect and IntersectClipRect for your convience
Code:Declare Function IntersectClipRect Lib "gdi32" Alias "IntersectClipRect" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Declare Function IntersectRect Lib "user32" Alias "IntersectRect" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
Code:If Abs(PicLaser.Left - picEnemy.Left) < PicLaser.Width And Abs(PicLaser.Top - picEnemy.Top) < PicLaser.Height Then 'collided
Thanks for the code-I used the IntersectRect API function and it worked just fine.
P.S.
I am not a begginer just a bit rusty because of learning other languages at this time so some things get messed up for me.