1
我正在制作一款可以与Agar.io中的相机进行比较的相机设置游戏。它可以上,下,左,右。但是,在Agar.io中,您仅限于地图空间。如果你碰到地图的一边,那么你必须回去。Swift:包围/循环/重复节点在摄像机边界之外?
但是,在我的游戏中,我希望相机'包装'到地图的另一面。
我找不到任何的例子,所以我做了我自己:
// This is in an SKScene
private func wrapNodes() {
let camPos = camera!.position // SKCameraNode of current scene
for node in self.children {
let nodePos = node.position
let x = camPos.x - nodePos.x
if x > spawnRect.width * 0.5 {
node.position.x = nodePos.x + spawnRect.width // spawnRect = map size
} else if x < -spawnRect.width * 0.5 {
node.position.x = nodePos.x - spawnRect.width // spawnRect = map size
}
let y = camPos.y - nodePos.y
if y > spawnRect.height * 0.5 {
node.position.y = nodePos.y + spawnRect.height // spawnRect = map size
} else if y < -spawnRect.height * 0.5 {
node.position.y = nodePos.y - spawnRect.height // spawnRect = map size
}
}
}
令人惊讶,由于我可怕的数学技能,这实际上似乎工作。
但是,这有两个问题。
首先是我根本不相信自己,我觉得我一定在某个地方犯了错。
第二个是需要相当长的时间才能遍历场景中的所有节点,所以我希望能有更快的方式,但我找不到一个。所以,我想知道是否有人知道更快的方法来做到这一点。
任何想法,将不胜感激!