0

I am making a game for iOS. I inputed two images, one will be used as the player, and the other which is an object. I want to code that if the two objects intersect, then it runs the method EndGame.

-(void)Collision{
    if (CGRectIntersectsRect(Ball.frame, Platform1.frame)) {
        [self EndGame];
    }
}

Although, The images are not a square shape, but the UIImage is a square. Therefore when the two objects intersect it Ends the game, even if the two images look like they came close it still ends the game because of the incorrect collision detection. Do you have suggestions?

Can I change the image shape on XCODE or can I make it so if the image collides with a certain point on the players image?

Thanks.

kRiZ
  • 2,320
  • 4
  • 28
  • 39

1 Answers1

0

create a custom view and in draw rect create a bezier path. Something like this

UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:ball.center radius:ballRadius startAngle:0.0 endAngle:M_PI*2.0 clockwise:YES];

This will draw a circle view

EDIT: heres a sample - I'm not sure exactly what all the details are, so this is pretty generic

@interface ballView : UIView//create subclass of UIView

... @implementation ...

- (void)drawRect:(CGRect)rect {

  UIBezierPath *path = [UIBezierPath bezierPath];//create bezier path
[path addArcWithCenter:self.center radius:5.0 startAngle:0.0 endAngle:M_PI*2.0  clockwise:YES];//draw circle. You can change around the parameters to the way you like 
 [[UIColor orangeColor]setStroke];
    [path stroke];//draw line
MendyK
  • 1,643
  • 1
  • 17
  • 30
  • how would I create a custom view and would I include that code in the same method? – ADAM9611222 Nov 27 '14 at 03:40
  • Once I do this how do I add the new rect to my Collision method? – ADAM9611222 Nov 27 '14 at 05:37
  • The same way you created your `Ball.frame`. Its a UIView [check out the class reference](https://developer.apple.com/library/ios/documentation/Uikit/reference/UIView_Class/index.html) – MendyK Nov 27 '14 at 15:08
  • Thankyou, but what is the UIColor and orangeColor? – ADAM9611222 Nov 27 '14 at 17:30
  • just setting the color of the line. Take that line out if you want to. – MendyK Nov 27 '14 at 18:21
  • One more question I added that above code you gave me, instead of self I used Ball (because that was my image name). Now how does it automatically know that if the two images collide, it can only end the game if one image collides with the certain radius? – ADAM9611222 Nov 27 '14 at 21:18
  • And do I have to make a subview for every single image ? – ADAM9611222 Nov 27 '14 at 21:20
  • I'm not sure exactly what youre game is so its hard to answer those questions. but you should take a look at [this](http://stackoverflow.com/questions/6589265/how-to-draw-a-custom-uiview-that-is-just-a-circle-iphone-app), [this](http://stackoverflow.com/questions/8101111/how-to-add-image-to-view-programmatically) and [this](http://stackoverflow.com/questions/22366726/how-to-detect-collision-between-2-uiviews). Hope those help! – MendyK Nov 27 '14 at 21:38