给 SKNode 自己的物理

Give an SKNode its own physics

有没有办法让 SKNode 拥有自己的物理特性?我有一个 SKShapeNode 调用 "backGround",我将其用于大多数其他节点的父节点。我不断地向左移动"background",给人一种玩家在向前移动的错觉。但是,其中一个以 "backGround" 作为父节点的对象是一根挂着绳子的大头针。当背景向左加速时,有没有办法让绳子不来回摆动,因为绳子在加速或减速时往往会这样做?

编辑:这是我的代码:

func createRopeNode(pos: CGPoint) -> SKSpriteNode{
    let ropeNode = SKSpriteNode(imageNamed: "Ball")
    ropeNode.size = CGSize(width: 5, height: 5)
    ropeNode.physicsBody = SKPhysicsBody(rectangleOfSize: ropeNode.size)
    ropeNode.physicsBody?.affectedByGravity = true
    ropeNode.physicsBody?.collisionBitMask = 0
    ropeNode.alpha = 1
    ropeNode.position = CGPoint(x: pos.x + 0, y: pos.y)
    ropeNode.name = "RopePiece"
    let text = SKSpriteNode(imageNamed: "RopeTexture")
    ropeNode.zPosition = -5
    text.runAction(SKAction.rotateByAngle(atan2(-dx!, dy!), duration: 0))
    ropeNode.addChild(text)
    return ropeNode
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    if (!playerIsConnected){
        playerIsConnected = true
    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        dx = pin.position.x - playerPoint!.x
        dy = pin.position.y - playerPoint!.y
        let length = sqrt(pow(dx!, 2) + pow(dy!, 2))
        let distanceBetweenRopeNodes = 5
        let numberOfPieces = Int(length)/distanceBetweenRopeNodes
        var ropeNodes = [SKSpriteNode]()

        //adds the pieces to the array at respective locations
        for var index = 0; index < numberOfPieces; ++index{
            let point = CGPoint(x: pin.position.x + CGFloat((index) * distanceBetweenRopeNodes) * sin(atan2(dy!, -dx!) + 1.5707), y: pin.position.y + CGFloat((index) * distanceBetweenRopeNodes) * cos(atan2(dy!, -dx!) + 1.5707))
            let piece = createRopeNode(point)
            ropeNodes.append(piece)
            world.addChild(ropeNodes[index])
        }
        let firstJoint = SKPhysicsJointPin.jointWithBodyA(ropeNodes[0].physicsBody, bodyB: pin.physicsBody, anchor:
            CGPoint(x: (ropeNodes[0].position.x + pin.position.x)/2, y: (ropeNodes[0].position.y + pin.position.y)/2))
        firstJoint.frictionTorque = 1
        self.physicsWorld.addJoint(firstJoint)
        for var i = 1; i < ropeNodes.count; ++i{
            let nodeA = ropeNodes[i - 1]
            let nodeB = ropeNodes[i]
            let middlePoint = CGPoint(x: (nodeA.position.x + nodeB.position.x)/2, y: (nodeA.position.y + nodeB.position.y)/2)
            let joint = SKPhysicsJointPin.jointWithBodyA(nodeA.physicsBody, bodyB: nodeB.physicsBody, anchor: middlePoint)
            joint.frictionTorque = 0.1
            self.physicsWorld.addJoint(joint)
        }
        finalJoint?.frictionTorque = 1
        finalJoint = SKPhysicsJointPin.jointWithBodyA(ropeNodes[ropeNodes.count - 1].physicsBody, bodyB: player.physicsBody, anchor:
            CGPoint(x: (ropeNodes[ropeNodes.count - 1].position.x + playerPoint!.x)/2, y: (ropeNodes[ropeNodes.count - 1].position.y + playerPoint!.y)/2))
        self.physicsWorld.addJoint(finalJoint!)
        }
    }
    else{
        physicsWorld.removeJoint(finalJoint!)
        playerIsConnected = false
    }
}

锚点就是您要找的。移动场景的锚点只移动场景的"camera"(屏幕上显示的内容)。这不会挤压销钉和绳索。请记住,锚点与场景的比例略有不同。

场景宽度可以为1024,一个场景长度的锚点的"width"为1(基本算作节点的一个宽度)。高度相同,它可能是 768,"height" 在锚点坐标 space 中仍然是 1。所以要移动半个屏幕宽度,将锚点移动 0.5

锚点是 CGPoint,因此您也可以垂直移动。这是一个简单的例子:

var xValue : Float = 0.75
var yValue : Float = 0.0
self.scene?.anchorPoint = CGPointMake(xValue, yValue);

为了进一步阅读,here's a link to the documentation 精灵的锚点。