`-costToNode:`不发送到GameplayKit中的GKGraphNode2D子类

我对苹果新的GameplayKit可能是一个愚蠢的问题。

我正在为我的游戏创build一个基于2D网格的节点布局。 我大多喜欢GKGraphNode2D的function,但是想要以一种方式调整它。 我想在遍历某种节点对时有条件地加罚。 换句话说,我希望一些节点以直接的方式连接起来,并且连接一些节点,使得它们的遍历距离由我的应用修改。

我认为GKGraphNode2D和覆盖-costToNode:-costToNode: -estimatedCostToNode:将完美的工作! 我会返回super有时提供的价值,并在其他时间调整我的游戏。 这两个方法是GKGraphNode的超类)上的GKGraphdNode2D

不幸的是,当我尝试这样做时,似乎在我的GKGraphNode2D子类中永远不会调用-costToNode:GKGraphNode2D 当我调用-findPathFromNode:toNode:在包含我的GKGraphNode2D子类对象的GKGraph对象上时,我希望调用这些方法。

我究竟做错了什么?

编辑:

以下是我的代码。

创build两个类, CheapNodeExpensiveNode ,它们是GKGraphNode2D子类,如下所示:

 @import UIKit; @import GameplayKit; @interface CheapNode : GKGraphNode2D @end @implementation CheapNode - (float)estimatedCostToNode:(GKGraphNode *)node { return 1; } - (float)costToNode:(GKGraphNode *)node { return 1; } @end 

 @import UIKit; @import GameplayKit; @interface ExpensiveNode : GKGraphNode2D @end @implementation ExpensiveNode - (float)estimatedCostToNode:(GKGraphNode *)node { return 100; } - (float)costToNode:(GKGraphNode *)node { return 100; } @end 

然后在testing中,我创build了一些节点,连接它们,将它们添加到一个图中,并将findPathFromNode:toNode:消息发送到graphics。 然后,我检查图表find的path,我没有find我期望的。

 - (void)testNodeCostsUsed { /* This is the graph we are creating: A---B---C | | F---E---D where all of the nodes are `CheapNode` objects, except 'B', which is an `ExpensiveNode` object. This test finds a path from node A to node C. If the cost methods in the `CheapNode` and `ExpensiveNode` subclasses are used, the route going from A to C around B (down, then right, then up) should be used, since the cost of going from B to C is 100, and the cost of going from any other node to any other node is 1 (see implementation of these classes). Instead, we see `GKGraph` choosing the "shortest" route in terms of number of nodes, from the top left immediately to the right to the top right. */ CheapNode *nodeA = [[CheapNode alloc] initWithPoint:(vector_float2){0, 0}]; ExpensiveNode *nodeB = [[ExpensiveNode alloc] initWithPoint:(vector_float2){1, 0}]; CheapNode *nodeC = [[CheapNode alloc] initWithPoint:(vector_float2){2, 0}]; CheapNode *nodeD = [[CheapNode alloc] initWithPoint:(vector_float2){2, 1}]; CheapNode *nodeE = [[CheapNode alloc] initWithPoint:(vector_float2){1, 1}]; CheapNode *nodeF = [[CheapNode alloc] initWithPoint:(vector_float2){0, 1}]; [nodeA addConnectionsToNodes:@[ nodeB, nodeF ] bidirectional:YES]; [nodeC addConnectionsToNodes:@[ nodeB, nodeD ] bidirectional:YES]; [nodeE addConnectionsToNodes:@[ nodeF, nodeD ] bidirectional:YES]; NSArray *allNodes = @[ nodeA, nodeB, nodeC, nodeD, nodeE, nodeF ]; GKGraph *graph = [GKGraph graphWithNodes:allNodes]; NSArray *nodes = [graph findPathFromNode:nodeA toNode:nodeC]; NSArray *expectedPath = @[ nodeA, nodeF, nodeE, nodeD, nodeC ]; NSArray *prohibitedPath = @[ nodeA, nodeB, nodeC ]; XCTAssert([nodes isEqualToArray:expectedPath], @""); XCTAssertFalse([nodes isEqualToArray:prohibitedPath], @""); } 

这个testing用例失败了。 我还注意到, estimatedCostToNode: costToNode: estimatedCostToNode:costToNode:从来没有发送到我的GKGraphNode2D子类(由日志语句和断点validation)。

这是一个演示这个问题的示例项目。 它应该build立和运行最新的Xcode开发人员testing版(截至2015年8月31日,Xcodetesting版6)。

编辑2:

我已经提交了示例代码和在这个StackOverflow问题的描述作为一个错误( http://www.openradar.me/22524760 )如果任何人有兴趣在复制。 如果错误在iOS 9发布后依然存在,我将使用开发人员支持服务单来尝试解决此问题。

编辑3:

苹果公司在我的开发者支持票上find了我。 他们承认这是一个错误,它(据我所知)似乎在iOS 9.2 beta 2(7C46t)中得到修复。 🎉

编辑4:

我已经更新了示例项目来说明这个框架的另一个错误 。

苹果回应了我与这个问题有关的开发者支持通知,并告诉我这是一个已知的问题,并且在2015年11月3日发布的iOS SDK 9.2 beta 2中有一个潜在的修复。我证实了我的testing项目通过9.2b2,所以我期望该版本来解决这个问题。