绑定相机到精灵界限

时间:2015-10-22 22:09:33

标签: objective-c swift sprite-kit skscene

我有一个场景摄像机跟随一个单位(精灵的位置),这个单位站在一个地形精灵,这个摄像机不仅跟随它,它还使用捏手势放大和缩小。一切都运作良好,除了我想限制相机的下限受地形精灵的下限限制。换句话说,我不希望相机在地形精灵下方显示任何内容。而且我的意思是相反,相机可以向上移动并保持比例。

任何快速,obj-c甚至伪代码的答案都会有所帮助。谢谢!

这是我的尝试

        let halfTerrain = terrain.size.height/2
        let halfViewport = scene!.size.height/2
        let distanceFromCenter = halfTerrain - cameraPosition.y
        let scaledDistanceFromCenter = distanceFromCenter/cameraScale
        let gapSize = halfViewport - halfTerrain/cameraScale
        let isOverlapping = scaledDistanceFromCenter < halfViewport

        if isOverlapping {
            camera?.runAction(SKAction.moveTo(CGPoint(x: cameraPosition.x,
                y: -gapSize * cameraScale), duration: movingSpeed))
        } else {
            camera?.runAction(SKAction.moveTo(cameraPosition,
                duration: movingSpeed))
        }
        camera?.runAction(SKAction.scaleTo(cameraScale,
            duration: scaleSpeed))

0 个答案:

没有答案