Subclass of GKGraphNode costToNode method never getting called

99封情书 提交于 2019-11-30 04:48:04

问题


I am trying to subclass GKGraphNode2D to also include different penalties for different terrains (in the costToNode method). When I create a new GKGraph with an array of my new subclass and call findPathFromNode on the GKGraph, it ignores my costToNode method entirely.

The code looks like:

public class PenaltyGraphNode: GKGraphNode2D {

    public let penalty: Float

    init(position: CGPoint, penalty: Float) {
        self.penalty = penalty
        let point = vector_float2(Float(position.x), Float(position.y))
        super.init(point: point)
    }

    override public func costToNode(node: GKGraphNode) -> Float {

        guard let penaltyNode = node as? PenaltyGraphNode else { return super.costToNode(node) }

        return super.costToNode(node) * (1 + penaltyNode.penalty)
    }
}

And where I am trying to use the GKGraph (node creation removed for brevity sake):

let penaltyNodes: [PenaltyGraphNode] = [penaltyNode1, penaltyNode2, ...]

let graph = GKGraph(nodes: penaltyNodes)
let path = graph.findPathFromNode(startNode, toNode: endNode) as? [PenaltyGraphNode]

The costToNode in my subclass is never getting called, I made sure that all of the nodes that I create in the graph and add to it later are all PenaltyGraphNodes, but it still won't call costToNode. When I print the graph's nodes property, it shows them as GKGraphNode2D objects only, not my subclass.

Am I missing something obvious here?

来源:https://stackoverflow.com/questions/32317628/subclass-of-gkgraphnode-costtonode-method-never-getting-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!