我在苹果提供的新框架中基本上尝试了一个带孔的矩形,唯一的问题是它的默认方法 bodyWithPolygonFromPath 不接受凹多边形,所以我尝试按如下方式解决问题:
CGPathMoveToPoint (path, NULL, self.size.width/4.0, self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, self.size.width/2.0, -self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, -self.size.height/4.0);
CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, self.size.height/4.0);
CGPathCloseSubpath (path);
self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
self.physicsBody.affectedByGravity = YES;
self.physicsBody.categoryBitMask = solidCategory;
self.physicsBody.dynamic = YES;
[self addChild:shape1];
self.auxiliaryShapeNode = [[SKSpriteNode alloc] init];
CGPathMoveToPoint (path_aux, NULL, 3*self.size.width/8.0, 0);
CGPathAddLineToPoint(path_aux, NULL, self.size.width/2.0, 1.5*self.size.height/4.0);
CGPathAddLineToPoint(path_aux, NULL, self.size.width/2.0, -self.size.height/4.0);
CGPathCloseSubpath (path_aux);
self.auxiliaryShapeNode.anchorPoint = CGPointMake(-3.0, 1.0/2.5);
self.auxiliaryShapeNode.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path_aux];
self.auxiliaryShapeNode.physicsBody.dynamic = YES;
self.auxiliaryShapeNode.physicsBody.categoryBitMask = solidCategory;
[self addChild:self.auxiliaryShapeNode];
[self.scene.physicsWorld addJoint:[SKPhysicsJointFixed jointWithBodyA:self.physicsBody bodyB:self.auxiliaryShapeNode.physicsBody anchor:self.position]];
break;
其中 self 是我创建的一个自定义精灵节点,它有一个 SKSprite 节点作为辅助身体形状,因此形状可以由两个凸多边形组成,它按预期创建形状,但是当场景开始播放关节时不会不能按预期工作,并将小三角形从开始的地方移开