gpt4 book ai didi

ios - CGPath 麻烦吗?

转载 作者:行者123 更新时间:2023-11-29 12:53:40 29 4
gpt4 key购买 nike

下面的代码有点问题。 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 的人不会这样做。

关于ios - CGPath 麻烦吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21710684/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com