ios – 在SpriteKit中围绕场景的背景夹紧相机

ios – 在SpriteKit中围绕场景的背景夹紧相机,第1张

概述所以我有一个基础游戏设置,可以在下面的bitbucket链接找到: Game link 我目前很难理解如何翻译相对于场景布局的相机节点. 目标是让摄像机跟随播放器,直到达到由场景大小定义的角落边界.在此特定测试场景设置中,场景大小为1000×1000,相机比例为1. 下面的代码用于在新位置设置为跟随角色时修改摄像机的位置: var cameraPosition: CGPoint { 所以我有一个基础游戏设置,可以在下面的bitbucket链接找到:

Game link

我目前很难理解如何翻译相对于场景布局的相机节点.

目标是让摄像机跟随播放器,直到达到由场景大小定义的角落边界.在此特定测试场景设置中,场景大小为1000×1000,相机比例为1.

下面的代码用于在新位置设置为跟随角色时修改摄像机的位置:

var cameraposition: CGPoint {        get {            return CGPoint(x: camera!.position.x,y: camera!.position.y)        }        set {            let cameRascale = CGfloat(1)            let scenescale = CGfloat(1)//CGfloat(1 - 0.44  + 0.05 /*possible rounding error adjustment*/)//            let vIEwaspectRatio = CGRectGetWIDth(vIEw!.frame)/CGRectGetHeight(vIEw!.frame)            let newpositionValue = double2(x: Double(newValue.x * scenescale),y: Double(newValue.y * scenescale))            let scaledScenesize = CGSize(wIDth: size.wIDth * scenescale,height: size.height * scenescale)////            scaledScenesize.height = scaledScenesize.height / vIEwaspectRatio            let cameraSize = vIEw!.bounds.size            let scaledCameraSize = CGSize(wIDth: cameraSize.wIDth * cameRascale,height: cameraSize.height * cameRascale)            let minX = 0//-scaledScenesize.wIDth * anchorPoint.x + scaledCameraSize.wIDth / 2            let minY = -219//-scaledScenesize.height * anchorPoint.y + scaledCameraSize.height / 2            let minValues = double2(x: Double(minX),y: Double(minY))            let maxX = 0//(scaledScenesize.wIDth * anchorPoint.x - scaledCameraSize.wIDth / 2) //size.wIDth - cameraSize.wIDth / 2            let maxY = 219//(scaledScenesize.height * anchorPoint.y - scaledCameraSize.height / 2) //- cameraSize.height / 2            let maxValues = double2(x: Double(maxX),y: Double(maxY))            let clampedposition = clamp(newpositionValue,min: minValues,max: maxValues)            camera!.position = CGPoint(x: (clampedposition.x / Double(scenescale)),y: (clampedposition.y / Double(scenescale)))        }    }

目前有适合所需场景大小的硬核值,我不确定如何通过比例获得这些结果.默认情况下,比例为:

/* Set the scale mode to scale to fit the window */            scene.scaleMode = .AspectFill

如果不知道有规模的翻译,默认情况下,我会期望边界
largestSceneDimensionXValue – cameraSize.wIDth / 2
largestSceneDimensionYValue – cameraSize.height / 2

作为一个高级的例子.有人能帮助我获得这个翻译吗?

整体而言,场景在所有角落应如下所示:

VS在相机中出现黑色背景溢出:

解决方法 像这样的应用正是070​​00的用途.

你可以看到这个确切功能的演示 – 约束一个摄像机,使其跟随播放器,但在WWDC15会话Deeper into GameplayKit with DemoBots中没有显示太多的空间 – 在WWDC15会话Deeper into GameplayKit with DemoBots中.*(那里的链接应跳转到大约在7:27开始讨论这个功能.)

视频中的内容,以及DemoBots示例代码中的一些片段:

>使用距离约束将相机保持在播放器的中央(自动,无需在每次更新()时直接设置camera.position).

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

>使用位置约束将摄像机保持在关卡边缘的特定范围内.通过获取关卡的框架并按照相机应该保持距离关卡边缘的距离来计算该范围.

// get the scene size as scaled by `scaleMode = .AspectFill`let scaledSize = CGSize(wIDth: size.wIDth * camera.xScale,height: size.height * camera.yScale)// get the frame of the entire level contentslet boardNode = childNodeWithname(WorldLayer.Board.nodePath)!let boardContentRect = boardNode.calculateAccumulatedFrame()// inset that frame from the edges of the level// inset by `scaledSize / 2 - 100` to show 100 pt of black around the level// (no need for `- 100` if you want zero padding)// use min() to make sure we don't inset too far if the level is smalllet xInset = min((scaledSize.wIDth / 2) - 100.0,boardContentRect.wIDth / 2)let yInset = min((scaledSize.height / 2) - 100.0,boardContentRect.height / 2)let insetContentRect = boardContentRect.insetBy(dx: xInset,dy: yInset)// use the corners of the inset as the X and Y range of a position constraintlet 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 = boardNode

>将两个约束应用于SKCameraNode.

camera.constraints = [playerBotLocationConstraint,levelEdgeConstraint]

为了更深入一点,download Apple’s DemoBots sample code project,它有很多评论和支持代码,我从上面的片段中删除,以防止这篇文章变得过长.相机约束的所有内容都在LevelScene.swift中的func setCameraConstraints()中.

*尽管有会话名称,但它不仅仅是GameplayKit ……它还展示了如何利用iOS 8 / OS X 10.11 / Xcode 7中引入的许多技术构建类似于全尺寸游戏的东西:App Thinning,new SpriteKit功能,ReplayKit等等.

总结

以上是内存溢出为你收集整理的ios – 在SpriteKit中围绕场景的背景夹紧相机全部内容,希望文章能够帮你解决ios – 在SpriteKit中围绕场景的背景夹紧相机所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1067202.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存