我正在尝试创建一个圆形体/形状并将其挂钩到UIImage,这是一个带有透明背景的.png,只是一个简单的圆形,大小为60 x 60像素。
我正在使用下面的代码来做它,并且它工作正常,除了在圆形图像周围似乎有一个看不见的更大区域(即圆形图像的两侧不能接触其他对象,如圆形体/形状较大。我不知道为什么会发生这种情况,因为我在所有测量中都使用了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.
}
答案 0 :(得分: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.
}