- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 SpriteKit 场景,其中背景 (SKSpriteNode) 的大小是场景帧的 2 倍。我添加了一个摄像头,当用户用手指平移和捏合时,它会四处移动。我实现了缩放和平移,但是,我需要相机不移动超过背景节点的边缘。
我尝试过使用物理体和边缘循环,但是没有用(除非我设置错误)。这是我目前拥有的代码和一些有助于传达信息的图像。我应该移动背景节点而不是相机吗?
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody!.categoryBitMask = SCENE_EDGE_CAT
self.physicsWorld.contactDelegate = self
mCamera = self.childNode(withName: "camera") as! SKCameraNode
mCamera.physicsBody = SKPhysicsBody(edgeLoopFrom: mCamera.frame)
mCamera.physicsBody?.collisionBitMask = self.SCENE_EDGE_CAT
mCamera.physicsBody!.contactTestBitMask = mCamera.physicsBody!.collisionBitMask
func panForTranslation(_ translation: CGPoint) {
let position = mCamera.position
let aNewPosition = CGPoint(x: position.x - translation.x, y: position.y - translation.y)
mCamera?.position = aNewPosition
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let positionInScene = touch?.location(in:self)
let previousPosition = touch?.previousLocation(in:self)
let translation = CGPoint(x: (positionInScene?.x)! - (previousPosition?.x)!, y: (positionInScene?.y)! - (previousPosition?.y)!)
panForTranslation(translation)
print(mCamera.position)
}
转向这个...
最佳答案
有几种方法可以做到这一点。
在 Apple 的示例游戏 DemoBots 中,他们对相机使用约束以跟随玩家。但是,如果玩家靠近屏幕边缘,摄像机就会停止跟随他。
/// Constrains the camera to follow the PlayerBot without approaching the scene edges.
private 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 playerNode = playerBot.renderComponent.node
let playerBotLocationConstraint = SKConstraint.distance(zeroRange, to: playerNode)
/*
Also constrain the camera to avoid it moving to the very edges of the scene.
First, work out the scaled size of the scene. Its scaled height will always be
the original height of the scene, but its scaled width will vary based on
the window's current aspect ratio.
*/
let scaledSize = CGSize(width: size.width * camera.xScale, height: size.height * camera.yScale)
/*
Find the root "board" node in the scene (the container node for
the level's background tiles).
*/
let boardNode = childNode(withName: WorldLayer.board.nodePath)!
/*
Calculate the accumulated frame of this node.
The accumulated frame of a node is the outer bounds of all of the node's
child nodes, i.e. the total size of the entire contents of the node.
This gives us the bounding rectangle for the level's environment.
*/
let boardContentRect = boardNode.calculateAccumulatedFrame()
/*
Work out how far within this rectangle to constrain the camera.
We want to stop the camera when we get within 100pts of the edge of the screen,
unless the level is so small that this inset would be outside of the level.
*/
let xInset = min((scaledSize.width / 2) - 100.0, boardContentRect.width / 2)
let yInset = min((scaledSize.height / 2) - 100.0, boardContentRect.height / 2)
// Use these insets to create a smaller inset rectangle within which the camera must stay.
let insetContentRect = boardContentRect.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: insetContentRect.minX, upperLimit: insetContentRect.maxX)
let yRange = SKRange(lowerLimit: insetContentRect.minY, upperLimit: insetContentRect.maxY)
// Constrain the camera within the inset rectangle.
let levelEdgeConstraint = SKConstraint.positionX(xRange, y: yRange)
levelEdgeConstraint.referenceNode = boardNode
/*
Add both constraints to the camera. The scene edge constraint is added
second, so that it takes precedence over following the `PlayerBot`.
The result is that the camera will follow the player, unless this would mean
moving too close to the edge of the level.
*/
camera.constraints = [playerBotLocationConstraint, levelEdgeConstraint]
}
如果您不使用约束(您似乎正在这样做),则必须手动添加检查以阻止相机更新。类似于此伪代码的内容
func panForTranslation(_ translation: CGPoint) {
guard mCamera.position < ... else { return } // Check your corner position and exit early if camera moved to far
...
}
希望对你有帮助
关于swift - 如何在场景边缘停止 SKCameraNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42707550/
websocket的用途/场景 先总结:高即时性服务,比如聊天室的群聊,server顺序收到了张三,李四的消息,立即就推送给王五,不能让王五等半天。 Ajax也可以一秒一刷,让王五去问张三说话没,如果
前端的工作过程里,本地开发、提供测试环境,总得有个用着顺手的服务器软件,这个场景里nginx很流行。 介绍两个好用的配置项:rewrite try_files @xxxx rewrite 比较
我有一个场景的两个不同角度的 2 个视频文件,我想重建场景的 3D 估计。它类似于 3D 传感器的作用(例如 Kinect、PrimeSense)。我正在寻找一个库,甚至是一个完善的机器视觉算法,以便
我已阅读RebaseProject页面并尝试了一个不平凡的例子(不是对一个完整的分支进行 rebase )。这与 rebase D 的情况类似我场景B。 这是rebase之前的情况: default
有没有办法将我的场景保存在 JavaFx 应用程序中单独的 Java 文件中?我尝试过这样的事情: public class MyApp extends Application { pri
我有这样的场景:用户想要查看大量有关自己的信息。例如:年龄、姓名、地位、收入、工作、爱好、 child 的名字、妻子的名字、酋长的名字、祖父/祖母的名字。大约 50 个变量。他可以选择任何变量来显示信
我希望有人能帮助我解决这个问题:我有一个包含条目的表。我想执行查询并根据模式获取得分最高的记录。模式将是:如果我的话按原样出现,那么该条目的分数将是最高的。如果该单词出现在句子中,则该条目的分数将低于
我正在尝试在我的应用程序委托(delegate)方法中实现一些逻辑。了解当前正在运行哪种场景将非常有帮助。 [[CCDirector sharedDirector] runningScene] 返回当
好的,这是一个有趣的。我有 2 个表:tbl_notes、tbl_notes_categories 简单地说,tbl_notes 有一个 categoryid,我将 2 个表与该 ID 相关联。所以,
我有一个使用并行运行的 Specflow、selenium、NUnit 的测试解决方案在 AssemblyInfo 中添加了这个:[程序集:Parallelizable(ParallelScope.F
我正在尝试弄清楚如何在 SpriteKit 中添加更多场景。如果我在 GameViewController 中使用 SpriteKit 生成的行 if let scene = GameScene.un
目录 1、业务背景 2、场景分析 3、流程设计 1、业务流程 2、导入流程
我是 Unity 的新手,所以修复起来可能非常简单。我使用了一个 3D Google SketchUp 模型,我想让玩家环顾模型。 super 简单。 我添加了 3D 平面,添加了相机并更新了设置以支
我需要标记要跳过的某些测试。但是,有些测试是参数化的,我只需要能够跳过某些场景。 我使用 py.test -m "hermes_only" 调用测试或 py.test -m "not hermes_o
我已经开始使用 SpecFlow 并想知道是否可以在规范之间重用场景 基本上我的想法是这样的(我可能从根本上是错误的:)) 我编写了一项功能来验证导航。 功能:导航 I should be able
在编写验证输入表单上的信息的 BDD 场景时,您将如何列出规则。 选项是: 1) 每个规则一个场景 2)场景大纲,每个领域和规则的例子 我们如何说某些不在特定字符集中的无效内容,例如: 鉴于我输入了一
我们如何使用 StoryQ 来测试预期出现异常的场景? 最佳答案 就实际代码而言,在测试代码的 .Then 部分,您需要创建一个 Action 或 Func 来确定正在测试的内容,然后在代码的 .Th
完成快速初学者努力通过点击按钮向场景添加节点。 我知道我可以使用点击手势来获取点击坐标并执行点击测试,然后在点击的 3D 空间中放置一个对象。但是,我想在设备屏幕的中央显示一个球体或十字准线,当点击屏
如何在表格中传递空格? Background: Given the following books |Author |(here several spaces)
我正在尝试从 Eric Haines' Standard Procedural Database (SPD) 渲染“mount”场景,但折射部分就是不想配合。我已经尝试了所有我能想到的方法来修复它。
我是一名优秀的程序员,十分优秀!