我有一个缩放为 1 的相机节点。当我运行游戏时,我希望它缩小(即缩小)但将“地板”保持在底部。我将如何将相机节点固定到场景的底部并有效地“向上”放大(难以解释)。所以场景的底部保持在底部,但其余部分缩小。
我尝试过 SKConstraints 但没有任何运气(我在 SpriteKit 很新)
func setConstraints(with scene: SKScene, and frame: CGRect, to node: SKNode?) {
let scaledSize = CGSize(width: scene.size.width * xScale, height: scene.size.height * yScale)
let boardContentRect = frame
let xInset = min((scaledSize.width / 2), boardContentRect.width / 2)
let yInset = min((scaledSize.height / 2), boardContentRect.height / 2)
let insetContentRect = boardContentRect.insetBy(dx: xInset, dy: yInset)
let xRange = SKRange(lowerLimit: insetContentRect.minX, upperLimit: insetContentRect.maxX)
let yRange = SKRange(lowerLimit: insetContentRect.minY, upperLimit: insetContentRect.maxY)
let levelEdgeConstraint = SKConstraint.positionX(xRange, y: yRange)
if let node = node {
let zeroRange = SKRange(constantValue: 0.0)
let positionConstraint = SKConstraint.distance(zeroRange, to: node)
constraints = [positionConstraint, levelEdgeConstraint]
} else {
constraints = [levelEdgeConstraint]
}
}
然后调用函数:
gameCamera.setConstraints(with: self, and: scene!.frame, to: nil)
(这是我正在关注的教程中的代码)“setConstraints”函数是 SKCameraNode 的扩展
我不确定这会给我正确的输出,但是当我运行代码进行缩放时,它只是从中间缩放并显示场景 .sks 文件的周围区域。
gameCamera.run(SKAction.scale(to: 0.2, duration: 100))
这是缩放 gameCamera 的代码
编辑:下面的答案几乎是我想要的,这是我更新的答案:
let scaleTo = 0.2
let duration = 100
let scaleTop = SKAction.customAction(withDuration:duration){
(node, elapsedTime) in
let newScale = 1 - ((elapsedTime/duration) * (1-scaleTo))
let currentScaleY = node.yScale
let currentHeight = node.scene!.size.height * currentScaleY
let newHeight = node.scene!.size.height * newScale
let heightDiff = newHeight - currentHeight
let yOffset = heightDiff / 2
node.setScale(newScale)
node.position.y += yOffset
}