情况
我正在使用 GKObstacleGraph 在以下 map 上进行寻路:
橙色区域是障碍物 (GKPolygonObstacle),而紫色点和线是自定义放置的节点 (GKGraphNode2D) 及其各自的连接,实体可以使用它们来传送穿过障碍物。
目标
我认为在方法上正确的是将紫色节点之间的成本设置为 0(因为它们是门户)。考虑到这一点,我首先将所有紫色节点子类为 DoorGraphNodes:
import SpriteKit
import GameplayKit
class DoorGraphNode: GraphNode {
var id: String!
var floor: Int!
var complement: DoorGraphNode!
init(position: CGPoint, id: String, floor: Int) {
self.id = id
self.floor = floor
let point = vector_float2(Float(position.x), Float(position.y))
super.init(point: point)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setComplement(doorNode: DoorGraphNode) {
self.complement = doorNode
}
}
然后,我将要添加到 GKObstaclesGraph 的所有 GKGraphNode2D 节点子类化为 GraphNode:
import SpriteKit
import GameplayKit
class GraphNode: GKGraphNode2D {
override func cost(to node: GKGraphNode) -> Float {
if let fromNode = self as? DoorGraphNode {
if let toNode = node as? DoorGraphNode {
if fromNode.complement == toNode {
return 0.0
}
}
}
return self.cost(to: node)
}
}
这样,当我调用障碍图.findPath(from: startNode, to: targetNode) 时,GameplayKit 理论上应该在两个连接的(互补)紫色 (DoorGraphNode) 节点之间调用 costToNode: 方法时接收到 0.0。
问题
但实际上,我收到 EXC_BAD_ACCESS 错误,控制台中没有详细信息:
我不认为我在逻辑上做错了什么。您知道为什么会发生这种情况吗?
最佳答案
GraphNode 类中的第 22 行正在调用自身。它应该调用它的父级。
return super.cost(to: node)
关于swift - 调用costToNode : in subclassed GKGraphNode2D with GameplayKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41506737/