iOS游戏 - 矢量碰撞

时间:2014-11-27 02:58:40

标签: ios objective-c xcode uiimageview collision-detection

我正在为iOS制作游戏。我输入了两个图像,一个用作播放器,另一个用作对象。我想编码,如果两个对象相交,那么它运行方法EndGame

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

虽然,图像不是方形,但UIImage是正方形。因此,当两个物体相交时它会结束游戏,即使两个图像看起来很接近,它仍会因为不正确的碰撞检测而结束游戏。你有什么建议吗?

我可以更改XCODE上的图像形状,或者如果图像与玩家图像上的某个点发生碰撞,我可以这样做吗?

感谢。

1 个答案:

答案 0 :(得分:0)

创建自定义视图并在绘制rect中创建贝塞尔曲线路径。像这样的东西

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

这将绘制圆形视图

编辑:以下是一个样本 - 我不确定所有细节到底是什么,所以这是非常通用的

@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