我最近一直在排练 Sprite Kit,遇到了一个非常奇怪的问题。缩放父节点时,通过 SKPhysicsJointPin 连接在一起的主体逐渐分开,然后关节断开。让我给你看图片。
这是正常状态:
这是放大后的样子:
这是缩小的时候:
如果你问我如何连接身体:我将棕色棒连接到蓝色节点中心的蓝色节点。任何想法我的问题是什么?
编辑:我最近发现当连接体不是动态时,关节不会断裂,并且一切都按预期工作。因此,例如,如果我使用 [SKPhysicsBody bodyWithEdgleLoopFromRect] 而不是 [SKPhysicsBody bodyWithRectangleOfSize] 为精灵创建物理体,则没有问题。但我需要身体是动态的。
这是我用来将物理连接到节点的代码。当然,这一切都是动态完成的。为了简洁起见,我只是硬编码。
-(void)attachPhysics{
//fixedComponentLeft & fixedComponentRight are two SKSprites
fixedComponentLeft.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:fixedComponentLeft.frame.size.width];
fixedComponentRight.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:fixedComponentLeft.size.width];
beam1.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:beam1.size];
joiningBody.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:joiningBody1.size.width];
[self.scene.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:fixedComponentLeft.physicsBody bodyB:beam1.physicsBody anchor:fixedComponentLeft.position]];
[self.scene.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:joiningBody.physicsBody bodyB:beam1.physicsBody anchor:beam1.endPoint]];
beam2.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:beam2.size];
[self.scene.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:joiningBody.physicsBody bodyB:beam2.physicsBody anchor:beam2.position]];
[self.scene.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:fixedComponentRight.physicsBody bodyB:beam2.physicsBody anchor:beam2.endPoint]];
}
在上面的代码中,beam1 和beam2 是SKSpriteNode 的子类的实例。默认情况下,锚点是 (0,0.5),我添加了一个名为 endPoint 的属性,它用作精灵上最右边的边缘点。