簡體   English   中英

如何使用SKPhysicsJointFixed連接兩個SKSpriteNode

[英]How to join two SKSpriteNodes using 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];

提前致謝。

這是我的工作代碼。

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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM