我有两个SKSpriteNodes,我想保持相同的位置。 newLeftObject和newLeftObjectBackground。制作它的最佳方法是使两个物体即使施加力也始终保持相同的位置。我尝试将它们的质量和密度设置为彼此相等,但它们在下降时仍然会有稍微不同的位置。谢谢,任何帮助表示赞赏!
func createNewObjectLeftAndMoveToCenter() {
colorBackground.removeFromParent()
blackBackground.removeFromParent()
var newLeftObject = SKSpriteNode()
var newRightObject = SKSpriteNode()
var newLeftObjectBackground = SKSpriteNode()
var newRightObjectBackground = SKSpriteNode()
var distance = sqrt(((colorBackground.size.width / -2) - (verticalTarget.position.x)) * ((colorBackground.size.width / -2) - (verticalTarget.position.x)))
var distanceToCenter = sqrt(((0 - (verticalTarget.position.x))) * (0 - (verticalTarget.position.x)))
newLeftObject.size = CGSize(width: distance, height: colorBackground.size.height)
newLeftObject.position = CGPoint(x: verticalTarget.position.x - (newLeftObject.size.width / 2), y: colorBackground.position.y)
newLeftObject.color = colorBackground.color
newLeftObject.zPosition = 30
addChild(newLeftObject)
newLeftObjectBackground.size = CGSize(width: distance + 14, height: blackBackground.size.height)
newLeftObjectBackground.position = CGPoint(x: verticalTarget.position.x - (newLeftObject.size.width / 2), y: colorBackground.position.y)
newLeftObjectBackground.color = UIColor.black
newLeftObjectBackground.zPosition = 20
addChild(newLeftObjectBackground)
newRightObject.size = CGSize(width: distanceToCenter + colorBackground.size.width / 2, height: colorBackground.size.height)
newRightObject.position = CGPoint(x: verticalTarget.position.x + (newRightObject.size.width / 2), y: colorBackground.position.y)
newRightObject.color = colorBackground.color
newRightObject.zPosition = 40
addChild(newRightObject)
newRightObjectBackground.size = CGSize(width: (distanceToCenter + colorBackground.size.width / 2) + 14, height: blackBackground.size.height)
newRightObjectBackground.position = CGPoint(x: verticalTarget.position.x + (newRightObject.size.width / 2), y: colorBackground.position.y)
newRightObjectBackground.color = blackBackground.color
newRightObjectBackground.zPosition = 35
addChild(newRightObjectBackground)
newLeftObject.physicsBody = SKPhysicsBody(rectangleOf: newLeftObject.size)
newLeftObject.physicsBody?.isDynamic = true
newLeftObject.physicsBody?.affectedByGravity = true
newLeftObject.physicsBody?.allowsRotation = true
newLeftObject.physicsBody?.collisionBitMask = 0
newLeftObject.physicsBody?.density = 1
newLeftObject.physicsBody?.mass = 1
newLeftObject.physicsBody?.applyAngularImpulse(1)
newLeftObject.physicsBody?.applyForce(CGVector(dx: -10000, dy: 10000))
newLeftObjectBackground.physicsBody = SKPhysicsBody(rectangleOf: newLeftObjectBackground.size)
newLeftObjectBackground.physicsBody?.isDynamic = true
newLeftObjectBackground.physicsBody?.affectedByGravity = true
newLeftObjectBackground.physicsBody?.collisionBitMask = 0
newLeftObjectBackground.physicsBody?.density = 1
newLeftObjectBackground.physicsBody?.mass = 1
newLeftObjectBackground.physicsBody?.allowsRotation = true
newLeftObjectBackground.physicsBody?.applyAngularImpulse(1)
newLeftObjectBackground.physicsBody?.applyForce(CGVector(dx: -10000, dy: 10000))
newLeftObject.position = newLeftObjectBackground.position
var moveNewObjectToCenter = SKAction.moveTo(x: 0, duration: 0.25)
newRightObject.run(moveNewObjectToCenter)
newRightObjectBackground.run(moveNewObjectToCenter)
colorBackground = newRightObject
blackBackground = newRightObjectBackground
}
答案 0 :(得分:2)
使用addChild()
让一个孩子成为另一个孩子