问题
使用A *寻路技术
我正在开发一款游戏。寻路相当优化。但是,我必须对其进行调整以考虑过多/过少的区块,这会在考虑区块的邻居时增加循环次数。
所以作为一个快速解决方案,我决定在另一个线程上执行寻路,以便在游戏中自行停止冻结。
然而,现在当它进行寻路时,通过调试我已经注意到它会形成一条路径 - 然后是中间路径,它将移动到一个不可能的邻居。好像它丢失了当前位置的记忆并回到原来的位置左右。
为问题提供代码有点困难,因为有太多代码甚至无法找到确切的问题,我相信这是一个内存问题或指针丢失它的数据。
代码段
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self generateRandomPathForPlayer:player];
});
-(void)generateRandomPathForPlayer:(PlayerClass *)player {
if (!player.dead) {
int x = [self returnRandomNumberBetween:1 to:map.width];
int y = [self returnRandomNumberBetween:1 to:map.height];
for (Tile *block in self.children) {
if ([block isKindOfClass:[Tile class]]) {
if (CGPointEqualToPoint(block.point, CGPointMake(x, y))) {
if (block.blocked) {
break;
} else {
NSArray *path = [self findPathFrom:player.currentPoint to:block.point];
for (int i = 0; i < [path count]; i++) {
HSAIPathFindingNode *node = [path objectAtIndex:i];
[player.playerPath addObject:[NSValue valueWithCGPoint:node.point]];
}
}
}
}
}
}
}
-(NSArray*)findPathFrom:(CGPoint)start to:(CGPoint)end{
HSAIPathFinding *pathFinder = [[HSAIPathFinding alloc] init];
pathFinder.delegate = self;
pathFinder.heuristic = [HSAIPathFindingHeuristic diagonalHeuristic];
return [pathFinder findPathFrom: start to: end];; // returns an array of HSAIPathFindingNodes
}
路径(出错时)的日志示例如下:
2016-10-01 14:13:45.734 pathfinding[2725:244756] (
"NSPoint: {12, 2}",
"NSPoint: {13, 3}",
"NSPoint: {14, 3}",
"NSPoint: {15, 3}",
"NSPoint: {16, 3}",
"NSPoint: {17, 3}",
"NSPoint: {18, 3}",
"NSPoint: {19, 3}",
"NSPoint: {10, 2}",
"NSPoint: {11, 3}",
"NSPoint: {11, 4}",
"NSPoint: {11, 5}",
"NSPoint: {11, 6}",
"NSPoint: {11, 7}",
"NSPoint: {11, 8}",
"NSPoint: {11, 9}",
"NSPoint: {11, 10}",
"NSPoint: {11, 11}",
"NSPoint: {11, 12}",
"NSPoint: {11, 13}",
"NSPoint: {11, 14}",
"NSPoint: {11, 15}",
"NSPoint: {11, 16}",
"NSPoint: {12, 17}",
"NSPoint: {12, 18}",
"NSPoint: {12, 19}",
"NSPoint: {13, 20}",
"NSPoint: {13, 21}",
"NSPoint: {13, 22}",
"NSPoint: {13, 23}",
"NSPoint: {13, 24}",
"NSPoint: {12, 25}",
"NSPoint: {11, 26}",
"NSPoint: {11, 27}",
"NSPoint: {11, 28}",
"NSPoint: {10, 2}",
"NSPoint: {11, 3}",
"NSPoint: {11, 4}",
"NSPoint: {11, 5}",
"NSPoint: {12, 6}",
"NSPoint: {13, 7}",
"NSPoint: {13, 8}",
"NSPoint: {13, 9}",
"NSPoint: {13, 10}",
"NSPoint: {14, 11}",
"NSPoint: {15, 12}",
"NSPoint: {15, 13}",
"NSPoint: {15, 14}",
"NSPoint: {15, 15}",
"NSPoint: {15, 16}",
"NSPoint: {16, 17}",
"NSPoint: {17, 18}",
"NSPoint: {18, 19}",
"NSPoint: {19, 20}",
"NSPoint: {19, 21}",
"NSPoint: {20, 22}",
"NSPoint: {21, 23}",
"NSPoint: {22, 24}",
"NSPoint: {23, 24}",
"NSPoint: {24, 24}",
"NSPoint: {25, 24}",
"NSPoint: {26, 24}",
"NSPoint: {27, 24}",
"NSPoint: {28, 24}",
"NSPoint: {29, 25}",
"NSPoint: {29, 26}",
"NSPoint: {29, 27}",
"NSPoint: {28, 28}",
"NSPoint: {27, 28}",
"NSPoint: {26, 28}",
"NSPoint: {25, 29}"
)