如何使用SKPhysicsJointFixed连接两个SKSpriteNodes

时间:2014-06-19 07:06:17

标签: ios sprite-kit skspritenode skphysicsbody skphysicsjoint

当我尝试使用SKPhysicsJointFixed连接两个SKSpriteNodes时,它们会分开,好像根本没有连接。我所做的一切似乎都没有用。这是我的代码......

CGPoint position = CGPointMake(100, 100);
CGSize size = CGSizeMake(4, 64);

SKSpriteNode *node1 = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:size];
node1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:size];
node1.position = position;

SKSpriteNode *node2 = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:size];
node2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:size];
node2.position = position;

[scene addChild:node1];
[scene addChild:node2];

SKPhysicsJointFixed *joint = [SKPhysicsJointFixed jointWithBodyA:node1.physicsBody
                                                            bodyB:node2.physicsBody
                                                          anchor:CGPointMake(100, 100)];
[scene.physicsWorld addJoint:joint];

提前致谢。

1 个答案:

答案 0 :(得分:1)

这是我的工作代码......

CGPoint position = CGPointMake(100, 350);
CGPoint position2 = CGPointMake(100, 420);
CGSize size = CGSizeMake(4, 164);

SKSpriteNode *node1 = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:size];
node1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:size];
node1.position = position;

SKSpriteNode *node2 = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:size];
node2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:size];
node2.position = position2;

[scene addChild:node1];
[scene addChild:node2];

SKPhysicsJointFixed *joint = [SKPhysicsJointFixed jointWithBodyA:node1.physicsBody
                                                                   bodyB:node2.physicsBody
                                                                  anchor:CGPointMake(node1.position.x, node1.position.y-30)];

[scene.physicsWorld addJoint:joint];