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

标签 ios swift sprite-kit

我正在使用 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]
   }

这是顶部和底部缝隙的一些图片。

Top

bottom

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

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

最佳答案

我是这样学的:

//
// 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)]];

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

关于ios - SpriteKit - SKCameraNode 约束在顶部和底部留有空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33663751/

相关文章:

ios - 我可以从 GMSMarker 点数获取 UIScreen 点数吗?

ios - 如何为特定的 SKScene 使用 applicationWillTerminate?

ios - 使用大型 UIImage 数组时的内存问题导致崩溃(快速)

c# - iOS-Unity桥接——奇怪的句子

swift - 弹跳并提高球的速度

ios - 如何堆叠3个不同尺寸的按钮(swift/iOS)

swift - CGPath 的 PhysicsEditor 多边形路径

swift - 我如何给篮子它的确切形状,对于一个物理体,它是一个图像?

iphone - 从文本文件中读取 NSPoint

ios - C 库中使用数组的 UnsafeMutablePointer