SKPhysicsJointPin在侧面滚动Swift SpriteKit游戏

时间:2015-04-05 15:01:19

标签: swift sprite-kit side-scroller skphysicsjoint

我正在尝试在SpriteKit中开发一个侧滚动游戏。我的问题如下:

  • 我正在尝试为我的游戏添加一根绳子,但是当我开始游戏时它会中断。当我将这根绳子添加到非滚动游戏时,它的效果非常好。我认为这个问题来自现场的锚点或其他什么,但我无法弄明白。我已经有一个星期的“努力工作”了。我有一个名为“世界”的SKNode,并且所有对象都附加到它上面,我将“世界”移到左边,我将“相机”放在我的播放器上,所以玩家向右移动,所有其他对象都是留下了。

这是我的代码:

  • 以下是“世界”的功能

    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)
    }
    
  • 这是绳子的功能

    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)
        }
    }
    

我应该如何制作这根绳子,使其不再破裂?任何帮助,将不胜感激。谢谢!

0 个答案:

没有答案