0

我正在尝试创建一个圆形主体/形状并将其连接到 UIImage,这是一个具有透明背景的 .png,只是一个简单的圆形,大小为 60 x 60px。

我正在使用下面的代码来做,它工作正常,除了圆形图像周围似乎有一个不可见的较大区域(即圆形图像的侧面不能接触其他物体,如圆形体/形状更大。我不知道为什么会这样,因为我在半径等的所有测量中都使用了 60px。代码如下,知道有什么问题吗?

- (void) createCircleAtLocation: (CGPoint) location
{
    _button1 = [[UIButton alloc] init];
    _button1 = [UIButton buttonWithType: UIButtonTypeCustom];
    [_button1 setImage: [UIImage imageNamed: @"ball.png"] forState: UIControlStateNormal];
    _button1.bounds = CGRectMake(location.x, location.y, 60, 60);
    [self.view addSubview: _button1];

    float mass = 1.0;
    body1 = cpBodyNew(mass, cpMomentForCircle(mass, 60.0, 0.0, cpvzero));
    body1 -> p = location; // Center of gravity for the body
    cpSpaceAddBody(space, body1); // Add body to space

    cpShape *shape = cpCircleShapeNew(body1, 60.0, cpvzero);
    shape -> e = 0.8; // Set its elasticity
    shape -> u = 1.0; // And its friction
    cpSpaceAddShape(space, shape); // And add it.
}
4

1 回答 1

1

在这种情况下,“不可见区域”是形状。形状由其半径定义。如果你有一个 60x60 的图像,半径是 30 而不是 60。用新的半径更新形状和主体,它应该可以正常工作:

- (void) createCircleAtLocation: (CGPoint) location
{
    _button1 = [[UIButton alloc] init];
    _button1 = [UIButton buttonWithType: UIButtonTypeCustom];
    [_button1 setImage: [UIImage imageNamed: @"ball.png"] forState: UIControlStateNormal];
    _button1.bounds = CGRectMake(location.x, location.y, 60, 60);
    [self.view addSubview: _button1];

    float mass = 1.0;
    body1 = cpBodyNew(mass, cpMomentForCircle(mass, 30.0, 0.0, cpvzero));
    body1 -> p = location; // Center of gravity for the body
    cpSpaceAddBody(space, body1); // Add body to space

    cpShape *shape = cpCircleShapeNew(body1, 30.0, cpvzero);
    shape -> e = 0.8; // Set its elasticity
    shape -> u = 1.0; // And its friction
    cpSpaceAddShape(space, shape); // And add it.

}

于 2013-01-09T17:40:23.837 回答