iOS8 + SpriteKit。可以在重叠节点之间连接SKPhysicsJointSpring吗?

时间:2014-10-21 00:14:31

标签: ios8 sprite-kit

我有一个磁盘网格(纹理SKSpriteNodes)排列在背景矩形(纹理SKSpriteNode)上方的网格中: enter image description here

我在背景矩形和每个磁盘之间吸引了一个弹簧:

dotNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:scaleFactor * [dotNode radius]];

dotNode.physicsBody.categoryBitMask    = EIDotNodeCategory;
dotNode.physicsBody.collisionBitMask   = (EIDotNodeCategory | EISceneNodeCategory);
dotNode.physicsBody.contactTestBitMask = (EIDotNodeCategory | EISceneNodeCategory);

dotNode.physicsBody.dynamic = YES;

dotNode.physicsBody.density = 2.0;
dotNode.physicsBody.linearDamping = 0.01;
dotNode.physicsBody.restitution = 0.75;

SKNode *attachment = [dotNode.scene childNodeWithName:@"photoNode"];
SKPhysicsJointSpring *physicsJointSpring = [SKPhysicsJointSpring jointWithBodyA:attachment.physicsBody
                                                                          bodyB:dotNode.physicsBody
                                                                        anchorA:CGPointMake(dotNode.position.x, 8 + dotNode.position.y)
                                                                        anchorB:dotNode.position];

physicsJointSpring.damping = 0;
physicsJointSpring.frequency = 1;

[dotNode.scene.physicsWorld addJoint:physicsJointSpring];

以下是背景的物理设置:

PhotoNode *photoNode = [PhotoNode photoNodeWithName:@"photoNode" texture:self.photoTexture shader:shader];
photoNode.zPosition = 2;
photoNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:photoNode.size];

photoNode.physicsBody.categoryBitMask    = EIPhotoNodeCategory;
photoNode.physicsBody.collisionBitMask   = EISceneNodeCategory;
photoNode.physicsBody.contactTestBitMask = EISceneNodeCategory;

photoNode.physicsBody.dynamic = YES;
photoNode.physicsBody.allowsRotation = NO;
photoNode.physicsBody.pinned = YES;

以下是场景的物理设置:

// get physical
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

self.physicsBody.categoryBitMask    = EISceneNodeCategory;
self.physicsBody.collisionBitMask   = (EIDotNodeCategory | EIPhotoNodeCategory);
self.physicsBody.contactTestBitMask = (EIDotNodeCategory | EIPhotoNodeCategory);

当我运行应用程序时,我得到了这个:

enter image description here

没有弹簧行为。没有快乐。我错过了什么?

1 个答案:

答案 0 :(得分:0)

启用物理调试绘图。你会看到你的矩形是动态的身体随着圆圈一起移动。你应该使矩形静态(动态= NO),最好将它们创建为边缘循环形状。