SpriteKit - SKCameraNode约束在顶部和底部留出空间

时间:2015-11-12 03:09:03

标签: ios swift sprite-kit

我正在使用Apple提供的DemoBots示例来处理我的第一个SKCameraNode。我有限制在X轴上工作,但不是Y.我在顶部有间隙,即使我将它限制在最大和最小高度。

此外,我的相机在我的狗角色上被限制为0.0,但是当狗在世界各地移动时,它不会移动以使狗保持在相机的中心。

这是我的相机约束功能。我是否还需要在update中执行某些操作?

//MARK: Camera
    /// Constrains the camera to follow the Dog without approaching the scene edges.
    func setCameraConstraints() {
        // Don't try to set up camera constraints if we don't yet have a camera.
        guard let camera = camera else { return }

    // Constrain the camera to stay a constant distance of 0 points from the player node.
    let zeroRange = SKRange(constantValue: 0.0)
    let dogLocationConstraint = SKConstraint.distance(zeroRange, toNode: self.dog)

    // Set dog constraints to worldNode bounds.....
    let catConstraints = SKConstraint.distance(SKRange(lowerLimit: 0.0, upperLimit: self.size.height), toNode: self.worldNode)
    self.dog.constraints = [catConstraints]

    let scaledSize = CGSize(width: size.width * camera.xScale, height: size.height * camera.yScale)

    let worldContentRect = self.worldNode.calculateAccumulatedFrame()

    let xInset = min((scaledSize.width / 2), worldContentRect.width / 2)
    let yInset = min((scaledSize.height / 2), worldContentRect.height / 2)

    // Use these insets to create a smaller inset rectangle within which the camera must stay.
    let insetContentRect = worldContentRect.insetBy(dx: xInset, dy: yInset)

    // Define an `SKRange` for each of the x and y axes to stay within the inset rectangle.
    let xRange = SKRange(lowerLimit: xInset, upperLimit:xInset)
    let yRange = SKRange(lowerLimit: 0, upperLimit: insetContentRect.maxY)

    // Constrain the camera within the inset rectangle.
    let levelEdgeConstraint = SKConstraint.positionX(xRange, y: yRange)
    levelEdgeConstraint.referenceNode = self.worldNode

    camera.constraints = [dogLocationConstraint, levelEdgeConstraint]
   }

以下是顶部和底部间隙的一些图片。

Top

bottom

我该怎么做才能解决这个问题?

(由于NDA,我无法显示我的角色)。

0 个答案:

没有答案