我正在尝试创建可滚动的游戏地图(类似于“糖果迷恋地图导航”)。我正在尝试通过创建一个包含摄像机的不可见播放器节点(mapNavigator)来遵循Apple demo bots摄像机的教程。但是,我回来的数字似乎不正确。
有人可以澄清一下数学如何工作吗?
当前情况:
http://www.giphy.com/gifs/iDOEEgXqz12aqgIDXC
代码我试图用以下方式约束摄像机
override func didMove(to view: SKView) {
super.didMove(to: view)
addChild(mapNavigator)
mapNavigator.addChild(mapCamera)
camera = mapCamera
constraintCamera()
}
override func update(_ currentTime: TimeInterval) {
mapCamera.position = mapNavigator.position
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
}
let location = touch.location(in: self)
let previousLocation = touch.previousLocation(in: self)
mapNavigator.position.x -= location.x - previousLocation.x
mapNavigator.position.y -= location.y - previousLocation.y
}
private func constraintCamera() {
guard let camera = camera,
let mapTileNode = childNode(withName: "mapTileNode")
else { return }
let mapContentRect = mapTileNode.calculateAccumulatedFrame()
let scaledSize = CGSize(width: size.width * camera.xScale, height: size.height * camera.yScale)
let xInset = min(scaledSize.width / 2, mapContentRect.width / 2)
let yInset = min(scaledSize.height / 2, mapContentRect.height / 2)
let insetContentRect = mapContentRect.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)
levelEdgeConstraint.referenceNode = mapTileNode
camera.constraints = [levelEdgeConstraint]
mapNavigator.constraints = [levelEdgeConstraint]
}