[英]CGPath trouble?
下面的代码有点麻烦。 points数组是由寻路算法提供的一组点,该算法给出了在以下方法中创建的起点和终点CGPoints之间的最短路径。 调试此代码后,我知道它可以工作。
我认为是导致我出现问题的CGPath,似乎还没有清除? 每当我从算法中生成一条新路径时,播放器便始终退回到他最初开始的位置,然后他将沿着应用程序中到目前为止创建的每条路径的距离移动。 每当我尝试生成新路径时都会发生这种情况。
有任何想法吗?
-(void)doubleTap:(UITapGestureRecognizer *)touchPoint
{
CGPoint touchLocation = [touchPoint locationInView:touchPoint.view];
touchLocation = [self convertPointFromView:touchLocation];
//on double tap take the location of the player and the location tapped and pass it to the path finder.
CGPoint start = CGPointMake((int)(player.position.x/SPACING), (int)(player.position.y/SPACING));
CGPoint end = CGPointMake((int)(touchLocation.x/SPACING), (int)(touchLocation.y/SPACING));
NSMutableArray *points = [NSMutableArray arrayWithArray:[self reverseArray:[pathFinder findPath:start End:end]]];
//convert path to moveable path for sprite, move sprite along this path.
CGMutablePathRef path = CGPathCreateMutable();
if (points.count > 0)
{
PathFindingNode *firstNode = [points objectAtIndex:0];
CGPathMoveToPoint(path, NULL, firstNode.position.x, firstNode.position.y);
for (int i = 1; i < points.count; i++)
{
firstNode = [points objectAtIndex:i];
CGPathAddLineToPoint(path, NULL, firstNode.position.x, firstNode.position.y);
}
}
SKAction *hover = [SKAction followPath:path asOffset:NO orientToPath:YES duration:2.0];
[player runAction: [SKAction repeatAction:hover count:1]];
[points removeAllObjects];
CGPathRelease(path);
}
将路径传递给此代码时,某处存在内存泄漏:
//convert path to moveable path for sprite, move sprite along this path.
CGMutablePathRef path = CGPathCreateMutable();
if (points.count > 0)
{
PathFindingNode *firstNode = [points objectAtIndex:0];
CGPathMoveToPoint(path, NULL, firstNode.position.x, firstNode.position.y);
for (int i = 1; i < points.count; i++)
{
firstNode = [points objectAtIndex:i];
CGPathAddLineToPoint(path, NULL, firstNode.position.x, firstNode.position.y);
}
}
SKAction *hover = [SKAction followPath:path asOffset:NO orientToPath:YES duration:2.0];
[player runAction: [SKAction repeatAction:hover count:1]];
[points removeAllObjects];
CGPathRelease(path);
}
如果我将这段代码注释掉,则iPad上的内存大约为50mb。 如果未将其注释掉,那么它将一直越来越高,直到崩溃到1.5GB。
您发布的代码将创建一个新路径,并用点数组中的点填充它。 点数组始终包含以前的点加上新的点,或者场景工具箱方法followPath:asOffset:orientToPath:duration:将新路径附加到旧路径上。 (我实际上还没有使用过场景工具包,所以我不知道最后一种可能性。)
无论如何,您的CGPath处理代码看起来都不错。 您记得要CGRelease CGPath,很多只知道ARC的人不知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.