2014-06-19 25 views
1

當我嘗試連接兩個SKSpriteNodes與SKPhysicsJointFixed時,它們分開,就好像根本沒有連接。我做的沒有任何事情似乎有效。這裏是我的代碼...如何使用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]; 

在此先感謝。

+0

它的工作...但嘗試其他位置的節點2,讓我們說...... CGPoint形勢2 = CGPointMake(100,120); ... node2.position = position2; – TonyMkenu

+0

什麼不起作用?請檢查我的代碼...但你的代碼也可以,只需更改節點的顏色:) – TonyMkenu

+0

你說得對。它確實有效。謝謝。 – 0x141E

回答

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]; 
相關問題