1

I'm trying to develop a side scrolling game in SpriteKit. My problem is the following:

  • I'm trying to add a rope to my game, but it breaks when I start the game. When I add this rope to a non-scrolling game it works perfectly well. I think the problem is from the scene anchor point or something but I just can't figure it out. I have already a week of this "hard work". I have a SKNode named "world" and all the objects are attached to it and I move the "world" to the left and I center the "camera" on my player and so the player moves to the right and all the other objects are left behind.

Here is my code:

  • Here is the function for the "world"

    override func didSimulatePhysics() {
       centerOnNode(ball)    
    }
    
    func centerOnNode(node: SKNode) {
       positionInScene = convertPoint(node.position, fromNode: node.parent!)
       world.position = CGPointMake(world.position.x - positionInScene.x + 100, world.position.y)
    }
    
  • And here is the function for the rope

    let length = 5
    let startPosition = CGPointMake(CGRectGetMaxX(self.frame), CGRectGetMaxY(self.frame))
    var chains : [SKNode] = []
    
    func addChain() {
    
        let chainHolder = SKSpriteNode(imageNamed: "chainHolder")
        chainHolder.position = startPosition
    
        chains.append(chainHolder)
        world.addChild(chainHolder)
    
        chainHolder.physicsBody = SKPhysicsBody(circleOfRadius: chainHolder.size.width / 2)
        chainHolder.physicsBody?.dynamic = false
    
        for i in 0..<length {
    
            let chainRing = SKSpriteNode(imageNamed: "chainRing")
            let offset = chainRing.size.height * CGFloat(i + 1)
            chainRing.position = CGPointMake(startPosition.x, startPosition.y - offset)
            chainRing.name = String(i)
    
            chains.append(chainRing)
            world.addChild(chainRing)
    
            chainRing.physicsBody = SKPhysicsBody(rectangleOfSize: chainRing.size)
        }
    
        for i in 1...length {
    
            let nodeA = chains[i - 1]
            let nodeB = chains[i]
            let joint = SKPhysicsJointPin.jointWithBodyA(nodeA.physicsBody, bodyB: nodeB.physicsBody,
                anchor: CGPointMake(CGRectGetMidX(nodeA.frame), CGRectGetMinY(nodeA.frame)))
    
            /*var pt = CGPointMake(world.position.x, world.position.y);
            pt = convertPoint(pt, fromNode: world);
    
            pt.x += scene.size.width * scene.anchorPoint.x;
            pt.y += scene.size.height * scene.anchorPoint.y;
            //pt.x += nodeA.position.x //- nodeA.frame.size.width / 2
            //pt.y += nodeA.position.y
    
            let joint = SKPhysicsJointPin.jointWithBodyA(nodeA.physicsBody, bodyB: nodeB.physicsBody,
                anchor: pt)*/
    
            self.physicsWorld.addJoint(joint)
        }
    }
    

How should I create this rope so that it doesn't break anymore? Any help would be appreciated. Thanks!

Eduard
  • 620
  • 9
  • 21

0 Answers0