我尝试过使用物理体和边缘循环,但这不起作用(除非我设置错误).这是我目前拥有的代码和一些有助于传达信息的图像.我应该移动背景节点而不是相机吗?
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)self.physicsBody!.categoryBitMask = SCENE_EDGE_CATself.physicsWorld.contactDelegate = selfmCamera = self.childNode(withname: "camera") as! SKCameraNodemCamera.physicsBody = SKPhysicsBody(edgeLoopFrom: mCamera.frame)mCamera.physicsBody?.collisionBitMask = self.SCENE_EDGE_CATmCamera.physicsBody!.contactTestBitMask = mCamera.physicsBody!.collisionBitMaskfunc 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)}
场景(灰色是背景图像,红色是相机)
平移到这……
但我需要它停在边缘……
在Apples示例游戏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全部内容,希望文章能够帮你解决swift – 如何在场景边缘停止SKCameraNode所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)