Reputation:
I have done a bunch of searches but i was unsure what answers apply to my problem. Im making a simple game where on a timer a image moves down while you move your "character" left and right. How would i go about making it subtract 1 from a variable when they collide? I can post code if needed
Upvotes: 0
Views: 405
Reputation: 290
Are you talking about collision detection? I would probably need code, but I am assuming you know the width and height, x and y position of your objects, and that they are rectangular. Checking if they intersect should be trivial. Check if object 1's (y + height) is greater than object 2's (y + height) and that object 1's (x + width) is greater that object 2's x. Do similar things for other edges.
EDIT: In fact, you could run collision detection on another thread just do it doesn't bog down your UI thread.
Upvotes: 2
Reputation: 17321
It depends on the shape of the characters. For a simple case of both being circles, you could just find the distance between the centers at every step, and triggering the collision when it reaches less than the sum of radii. For the case of rectangular shapes, it would also be pretty simple.
Upvotes: 0