下面的代码有点问题。点数组是由路径查找算法提供的一组点,它给出了在下面的方法中生成的起点和结束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上的内存将保持在50 an左右。如果它没有被注释掉,那么它就会越来越高,直到它在1.5gb左右崩溃。
发布于 2014-02-11 19:09:40
您发布的代码将创建一个新路径,并将其填充到点数组中的各个点中。无论是您的点数组总是包含前面的点加上您的新点,还是场景工具包方法跟随路径:asOffset:orientToPath:D时程:是将新路径追加到旧路径。(我还没有真正使用过现场工具包,所以我不知道最后的可能性。)
无论如何,您的CGPath处理代码看起来不错。你记得CGRelease CGPath,很多只知道ARC的人不知道。
https://stackoverflow.com/questions/21710684
复制相似问题