Ios `-costToNode:`未发送到GameplayKit中的GKGraphNode2D子类
我有一个关于苹果新游戏工具包的愚蠢问题 我正在为我的游戏创建一个基于二维网格的节点布局。我最喜欢Ios `-costToNode:`未发送到GameplayKit中的GKGraphNode2D子类,ios,gameplay-kit,Ios,Gameplay Kit,我有一个关于苹果新游戏工具包的愚蠢问题 我正在为我的游戏创建一个基于二维网格的节点布局。我最喜欢GKGraphNode2D的功能,但我想用一种方式对其进行调整。我想在遍历某种节点对时有条件地增加一个惩罚。换句话说,我希望一些节点以一种直接的方式连接,而一些节点的连接使得我的应用程序可以修改它们的遍历距离 我认为子类化GKGraphNode2D并重写-costToNode:和-estimatedCostToNode:将非常有效!有时我会返回super提供的值,然后在其他时候为我的游戏调整它。这两种
GKGraphNode2D
的功能,但我想用一种方式对其进行调整。我想在遍历某种节点对时有条件地增加一个惩罚。换句话说,我希望一些节点以一种直接的方式连接,而一些节点的连接使得我的应用程序可以修改它们的遍历距离
我认为子类化GKGraphNode2D
并重写-costToNode:
和-estimatedCostToNode:
将非常有效!有时我会返回super
提供的值,然后在其他时候为我的游戏调整它。这两种方法是gkgraphnode2d
的超类GKGraphNode
上的方法
不幸的是,当我尝试这样做时,似乎从未在我的GKGraphNode2D
子类中调用-costToNode:
和-estimatedCostToNode:
。我希望在调用GKGraph
对象上的-findPathFromNode:toNode:
时调用这些方法,该对象包含一组GKGraphNode2D
子类对象
我做错了什么
编辑:
下面是我的代码
创建两个类,CheapNode
和ExpensiveNode
,它们是GKGraphNode2D
的子类,如下所示:
@import UIKit;
@import GameplayKit;
@interface CheapNode : GKGraphNode2D
@end
@implementation CheapNode
- (float)estimatedCostToNode:(GKGraphNode *)node {
return 1;
}
- (float)costToNode:(GKGraphNode *)node {
return 1;
}
@end
及
然后在一个测试中,我创建了一些节点,将它们连接起来,将它们添加到一个图中,并将findPathFromNode:toNode:
消息发送到该图中。然后,我检查了图形找到的路径,但没有找到我所期望的
- (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], @"");
}
此测试用例失败。我还注意到,estimatedCostToNode:
和costToNode:
从未发送到我的GKGraphNode2D
子类(通过日志语句和断点验证)
是演示此问题的示例项目。它应该在最新的开发者测试版Xcode上构建和运行(截至2015年8月31日,Xcode测试版6)
编辑2:
如果有人对复制感兴趣,我已经将StackOverflow问题中的示例代码和描述作为bug()提交。如果在iOS 9发布后bug仍然存在,我将使用开发人员支持票证来尝试解决此问题
编辑3:
苹果在我的开发者支持票上回复了我。他们承认这是一个bug,而且它(据我所知)似乎在iOS9.2Beta2(7C46t)中得到了修复 苹果回复了我关于这个问题的开发者支持票,并告诉我这是一个已知的问题,并且在2015年11月3日发布的iOS SDK 9.2 beta 2中有一个潜在的修复程序。我验证了我在9.2b2中通过的测试,因此我希望该版本能够解决此问题。好吧,您明显做错的一件事是没有发布代码,以便有人可以检查代码是否存在任何(或多或少明显的)问题。;)我只能告诉你,这是正确的方法,它对我来说是有效的(网格图)。我有一个类似的问题,我在这里问过:包括我现在的代码using@LearnCocos2D示例代码发布!你有更新吗?我对此也很感兴趣。@Weston检查问题的最新编辑,这个错误应该在9.2 beta 2I测试中修复,这一点得到了确认
- (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], @"");
}