我正在尝试在SpriteKit中开发一个侧滚动游戏。我的问题如下:
这是我的代码:
以下是“世界”的功能
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)
}
}
我应该如何制作这根绳子,使其不再破裂?任何帮助,将不胜感激。谢谢!