不久前,苹果在 Xcode 的 Scene Editor 中引入了 GameplayKit 元素,非常棒。但是,我似乎对 Navigation Graph 元素有疑问:
我试图实现的是从场景编辑器中绘制一个 GKGraph,在代码中检索它,并将其用作寻路的基础。
所以我先画一个GKGraph:
然后我像这样在 GameScene.swift 中检索它:
graph = self.graphs.values.first
其中graphs是 Apple 预定义的数组,用于存储来自场景编辑器的 GKGraphs。到目前为止,一切都很好。
然后我想让玩家在屏幕上找到点击位置的路径。为此,我将UITapGestureRecognizer与以下回调方法一起使用:
var moving = false
func moveToLocation(recognizer: UITapGestureRecognizer) {
var tapLocation = recognizer.location(in: recognizer.view!)
tapLocation = self.convertPoint(fromView: tapLocation)
guard (!moving) else { return }
moving = true
let startNode = GKGraphNode2D(point: vector_float2(Float(player.visualComponent.node.position.x), Float(player.visualComponent.node.position.y)))
let endNode = GKGraphNode2D(point: vector_float2(Float(tapLocation.x), Float(tapLocation.y)))
NSLog(graph.nodes.debugDescription)
graph.connectToLowestCostNode(node: startNode, bidirectional: true)
graph.connectToLowestCostNode(node: endNode, bidirectional: true)
NSLog(graph.nodes.debugDescription)
let path: [GKGraphNode] = graph.findPath(from: startNode, to: endNode)
guard path.count > 0 else { moving = false; return }
var actions = [SKAction]()
for node: GKGraphNode in path {
if let point2d = node as? GKGraphNode2D {
let point = CGPoint(x: CGFloat(point2d.position.x), y: CGFloat(point2d.position.y))
let action = SKAction.move(to: point, duration: 1.0)
actions.append(action)
}
}
graph.remove([startNode, endNode])
// Convert those actions into a sequence action, then run it on the player node.
let sequence = SKAction.sequence(actions)
player.visualComponent.node.run(sequence, completion: { () -> Void in
// When the action completes, allow the player to move again.
self.moving = false
})
}
使用这种方法,我让玩家去点击位置。问题是 GameplayKit 返回的路径只包含startNode和endNode,这意味着玩家根本没有在预先绘制的 GKGraph 上行走,而是直接走到endNode。
注意:当我第一次运行NSLog(graph.nodes.debugDescription)时,它会列出所有 10 个预先绘制的节点。当我在graph.connectToLowestCostNode之后再次运行它时,我得到了所有 12 个节点。所以我的图表似乎包含了寻路所需的一切。
你认为是什么问题?为什么graph.findPath不使用预先绘制的 10 个节点来定义路径?您的回答将不胜感激。