【问题标题】:SpriteKit - SKCameraNode constraints leave space at top and bottomSpriteKit - SKCameraNode 约束在顶部和底部留下空间
【发布时间】:2016-12-03 11:18:10
【问题描述】:

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

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

这是我的相机约束功能。我还需要在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]
   }

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

我能做些什么来解决这个问题?

(由于保密协议,我无法展示我的角色)。

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    我是这样学会的:

    //
    // Create our camera
    //
    SKNode *avatar = [self childNodeWithName:@"avatar"];
    SKCameraNode *camera = (SKCameraNode *) [self childNodeWithName:@"camera"];
    
    id horizConstraint = [SKConstraint distance:[SKRange rangeWithUpperLimit:100]
                                         toNode:avatar];
    id vertConstraint = [SKConstraint distance:[SKRange rangeWithUpperLimit:50]
                                        toNode:avatar];
    
    id leftConstraint = [SKConstraint positionX:[SKRange rangeWithLowerLimit:camera.position.x]];
    id bottomConstraint = [SKConstraint positionY:[SKRange rangeWithLowerLimit:camera.position.y]];
    id rightConstraint =
    
        [SKConstraint positionX:[SKRange rangeWithUpperLimit: (backgroundboundary.frame.size.width - camera.position.x)]];
    id topConstraint =
        [SKConstraint positionY:[SKRange rangeWithUpperLimit: (backgroundboundary.frame.size.height - camera.position.y)]];
    

    如果您像我一样使用这种技术,请检查您在游戏场景中的相机位置。不可移动的十字准线是坐标系的零、零点。确保您的背景和相机相对于它定位。

    【讨论】:

    • 这也是我最终解决的方法。忘记回来编辑答案了。
    猜你喜欢
    • 1970-01-01
    • 2016-05-08
    • 2017-11-23
    • 2013-08-25
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    相关资源
    最近更新 更多