一个节点有两个物理体路径是可能的吗?我想要创建一个节点,该节点的两侧有两个(圆形)物理体。
如果这是不可能的,是否有任何解决办法来实现这一点?谢谢

发布于 2014-08-07 22:03:11
您想要使用[SKPhysicsBody bodyWithBodies:...]。来自docs:
利用传递给该方法的物理体的形状来创建一个新的物理体,其覆盖区域是其子女区域的结合。这些地区不需要是毗连的。如果两个部分之间有空间,其他机构可能能够通过这些部分之间。然而,物理体被视为一个单一的连接体,这意味着施加在身体上的力或冲力会影响到所有的物体,就好像它们与一个不可摧毁的框架结合在一起一样。
看起来会是这样的:
SKPhysicsBody *leftCircle = [SKPhysicsBody bodyWithCircleOfRadius:leftCircleRadius center:leftCircleCenter];
SKPhysicsBody *rightCircle = [SKPhysicsBody bodyWithCircleOfRadius:rightCircleRadius center:rightCircleCenter];
node.physicsBody = [SKPhysicsBody bodyWithBodies:@[leftCircle, rightCircle]];发布于 2014-08-07 17:37:37
下面是一个使用SKPhysicsJointFixed连接到sprite节点的示例。首先,创建两个精灵:
SKSpriteNode *sprite1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(64, 64)];
// position must be set before creating physics body to avoid bug in iOS 7.0.x
sprite1.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
sprite1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite1.size];
sprite1.physicsBody.restitution = 1.0;
[self addChild:sprite1];
SKSpriteNode *sprite2 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(64, 64)];
sprite2.position = CGPointMake(CGRectGetMidX(self.frame)-sprite2.size.width*2,
CGRectGetMidY(self.frame));
sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite2.size];
sprite2.physicsBody.restitution = 1.0;
[self addChild:sprite2];然后通过调用此方法连接节点:
[self connectNode1:sprite1 toNode2:sprite2];该方法将两个节点连接到它们的中点。请注意,在调用此方法之前,两个物理体都必须在场景中。
- (void) connectNode1:(SKSpriteNode *)node1 toNode2:(SKSpriteNode *)node2
{
CGPoint midPoint = CGPointMake((node1.position.x + node2.position.x)/2,
(node1.position.y + node2.position.y)/2);
SKPhysicsJointFixed *joint = [SKPhysicsJointFixed jointWithBodyA:node1.physicsBody
bodyB:node2.physicsBody
anchor:midPoint];
[self.physicsWorld addJoint:joint];
}发布于 2014-08-07 20:42:03
下面是实现您正在寻找的行为的简单方法:
How to detect contact on different areas of a physicsbody
//Create SpriteNode1 & physicsBody
//Create SpriteNode2 & physicsBody
[SpriteNode1 addChild: SpriteNode2]您可以相对于SpriteNode2定位SpriteNode1。在SpriteNode1上执行的任何移动等也将移动SpriteNode2。
集:SpriteNode2.PhysicsBody.Dynamic=NO;
您还可以创建一个充当主对象的SpriteNode,并在发现更容易的情况下将SN1和SN2添加为子对象。
https://stackoverflow.com/questions/25178390
复制相似问题