显示到 CGPoint SpriteKit 的方向
Posted
技术标签:
【中文标题】显示到 CGPoint SpriteKit 的方向【英文标题】:Show direction to CGPoint SpriteKit 【发布时间】:2018-07-01 19:06:51 【问题描述】:当用户移动他的图标和相机时,我需要一个箭头(白色圆圈)来显示指向 CGPoint 的方向。
我的意思是箭头(白色圆圈)需要位于可见屏幕的边缘,并显示帮助用户返回关注的CGPoint的方式。
Demo gif
【问题讨论】:
到目前为止你尝试了什么? 【参考方案1】:你期待两件事:
在给定目标 CGPoint 位置的情况下将箭头放在正确的屏幕一侧 将箭头指向目标 CGPoint在您的 touchesMoved(_:) 方法中,您可以更新箭头旋转和位置,未经测试但原理应该有效:
private func placeArrow(at sourceNode: SKNode, for targetNode: SKNode)
//do not display arrow if already on screen
guard targetNode.position.x > cameraNode.position.x - screenSizeX/2
&& targetNode.position.x < cameraNode.position.x + screenSizeX/2
&& targetNode.position.y > cameraNode.position.y - screenSizeY/2
&& targetNode.position.y < cameraNode.position.y + screenSizeY/2
arrowNode.isHidden = true
return
//find arrow position, if on the left place to the left side, else on the right
//place at the medium y between the 2 points
let screenSizeX = UIScreen.main.bounds.width
let screenSizeY = UIScreen.main.bounds.height
let ymin = cameraNode.position.y - screenSizeY/2 + 10
let ymax = cameraNode.position.y + screenSizeY/2 - 10
let midY = (sourceNode.position.y + targetNode.position.y)/2
var clampedMidY = midY
if midY > ymax
clampedMidY = ymax
else if midY < ymin
clampedMidY = ymin
arrowNode.position = CGPoint(x: (targetNode.position.x < sourceNode.position.x) ? cameraNode.position.x - screenSizeX/2 : cameraNode.position.x + screenSizeX/2, y: clampedMidY)
//find arrow orientation
//see https://***.com/questions/38411494/rotating-a-sprite-towards-a-point-and-move-it-towards-it-with-a-duration
let v1 = CGVector(dx:0, dy:1)
let v2 = CGVector(dx: targetNode.position.x - sourceNode.position.x, dy: targetNode.position.y - sourceNode.position.y)
arrowNode.zRotation = atan2(v2.dy, v2.dx) - atan2(v1.dy, v1.dx)
【讨论】:
我把scene.frame.contains
改成:if targetNode.position.x > cameraNode.position.x - screenSizeX/2 && targetNode.position.x < cameraNode.position.x + screenSizeX/2 && targetNode.position.y > cameraNode.position.y - screenSizeY/2 && targetNode.position.y < cameraNode.position.y + screenSizeY/2 arrowNode.isHidden = true //print("hid") return
谢谢!
现在我换了let ymin = cameraNode.position.y - screenSizeY/2 + 10 let ymax = cameraNode.position.y + screenSizeY/2 - 10
以上是关于显示到 CGPoint SpriteKit 的方向的主要内容,如果未能解决你的问题,请参考以下文章