How to create a rope in SpriteKit? How to create a rope in SpriteKit? xcode xcode

How to create a rope in SpriteKit?


I am the author of that video. Because of the big demand for the source code I have released it on the Github.

You can find it here


I've also done a rope with single parts connected with PinJoints. I think it's the only way to display a "rope". I think in the video it's the same, you can see the single chain links. You don't even need so much jointed elements, just let the sprite overlap a bit the physics body to make it look very real. Heres my example method... just replace the image names n stuff...

+(void) addRopeJointItems:(CGPoint)leftStartPosition   countJointElements:(int)countJointElements                 game:(SKScene*)game {     int itemJointWidth = 25;     //Left Physics Anchor     SKSpriteNode * leftAnchor = [SKSpriteNode spriteNodeWithImageNamed:@"dummypixel_transparent.png"];     leftAnchor.position = CGPointMake(leftStartPosition.x, leftStartPosition.y);     leftAnchor.size = CGSizeMake(1, 1);     leftAnchor.zPosition = 2;     leftAnchor.physicsBody = [SKPhysicsBody                          bodyWithRectangleOfSize:                          leftAnchor.size];     leftAnchor.physicsBody.affectedByGravity = false;     leftAnchor.physicsBody.mass = 99999999999;     [game addChild:leftAnchor];     //add RopeElements     for (int i=0; i<countJointElements; i++)      {           SKSpriteNode * item = [SKSpriteNode spriteNodeWithImageNamed:@"suspensionrope.png"];           item.name = [NSString stringWithFormat:@"ropeitem_%d", i];           item.position = CGPointMake(leftStartPosition.x + (i*itemJointWidth) + itemJointWidth/2, leftStartPosition.y+60);           item.size = CGSizeMake(itemJointWidth + 5, 5);           item.zPosition = 2;           item.physicsBody = [SKPhysicsBody                        bodyWithRectangleOfSize:                        item.size];           item.physicsBody.categoryBitMask = kNilOptions;           [game addChild:item];         //Add Joint to the element before           SKPhysicsBody* bodyA;           if (i == 0)            {               bodyA = leftAnchor.physicsBody;           }            else            {               bodyA = [game childNodeWithName:[NSString stringWithFormat:@"ropeitem_%d", i-1]].physicsBody;           }           SKPhysicsJointPin* joint = [SKPhysicsJointPin jointWithBodyA:bodyA bodyB:item.physicsBody anchor:CGPointMake((item.position.x - item.size.width/2) + 5, item.position.y)];          [game.physicsWorld addJoint:joint];    }    //Right Physics Anchor    SKSpriteNode * rightAnchor = [SKSpriteNode spriteNodeWithImageNamed:@"dummypixel_transparent.png"];    rightAnchor.position = CGPointMake((leftStartPosition.x + (countJointElements*itemJointWidth)),                                   leftStartPosition.y+60);    rightAnchor.size = CGSizeMake(1, 1);    rightAnchor.zPosition = 2;    rightAnchor.physicsBody = [SKPhysicsBody                           bodyWithRectangleOfSize:                           rightAnchor.size];    rightAnchor.physicsBody.affectedByGravity = false;    rightAnchor.physicsBody.mass = 99999999999;    [game addChild:rightAnchor];    //Add the Last Joint    SKPhysicsJointPin* jointLast = [SKPhysicsJointPin jointWithBodyA:[game childNodeWithName:[NSString stringWithFormat:@"ropeitem_%d", countJointElements - 1]].physicsBody                                                           bodyB:rightAnchor.physicsBody                                                          anchor:rightAnchor.position];    [game.physicsWorld addJoint:jointLast];}


in swift

 func addRopeJointItems(leftStartPosition: CGPoint, numOfJoints countJointElements:Int, andScene game:SKScene ){    var itemJointWidth = 25    var leftAnchor = SKSpriteNode(imageNamed: "rope_ring.png")    leftAnchor.position = CGPointMake(leftStartPosition.x, leftStartPosition.y)    leftAnchor.size = CGSizeMake(1, 1);    leftAnchor.zPosition = 2;    leftAnchor.physicsBody = SKPhysicsBody(rectangleOfSize: leftAnchor.size)    leftAnchor.physicsBody?.affectedByGravity = false    leftAnchor.physicsBody?.mass = 999999999;    game.addChild(leftAnchor)   for index in 0...countJointElements {    var item = SKSpriteNode(imageNamed: "rope_ring.png")    item.name = "ropeitem_" + String(index)    item.position = CGPointMake(leftStartPosition.x + CGFloat((index * itemJointWidth)) + CGFloat(itemJointWidth / 2) , leftStartPosition.y + 60)    item.size = CGSizeMake(CGFloat(itemJointWidth + 5), 5);    item.zPosition = 2;    item.physicsBody = SKPhysicsBody(rectangleOfSize: item.size)    item.physicsBody?.categoryBitMask = 0;    game.addChild(item)    var bodyA = SKPhysicsBody()    if (index == 0)    {        bodyA = leftAnchor.physicsBody!;    }    else    {        var nameString = "ropeitem_" + String(index - 1)        var node = game.childNodeWithName(nameString) as SKSpriteNode        bodyA = node.physicsBody!    }    var joint = SKPhysicsJointPin.jointWithBodyA(bodyA, bodyB: item.physicsBody, anchor: CGPointMake((item.position.x - item.size.width/2) + 5, item.position.y))    game.physicsWorld.addJoint(joint)    }    var rightAnchor = SKSpriteNode(imageNamed: "rope_ring.png")    rightAnchor.position = CGPointMake(leftStartPosition.x + CGFloat((countJointElements * itemJointWidth)), CGFloat(leftStartPosition.y + 60))    rightAnchor.size = CGSizeMake(1, 1);    rightAnchor.zPosition = 2;    rightAnchor.physicsBody = SKPhysicsBody(rectangleOfSize: rightAnchor.size)    rightAnchor.physicsBody?.affectedByGravity = false    rightAnchor.physicsBody?.mass = 999999999;    game.addChild(rightAnchor)      var nameString = NSString(format: "ropeitem_%d", countJointElements - 1)    var node = game.childNodeWithName(nameString)      var jointLast = SKPhysicsJointPin.jointWithBodyA(node!.physicsBody!, bodyB: rightAnchor.physicsBody, anchor: rightAnchor.position)    game.physicsWorld.addJoint(jointLast)}