Ios 路径问题?

Ios 路径问题?,ios,objective-c,sprite-kit,cgpath,Ios,Objective C,Sprite Kit,Cgpath,下面的代码有点小问题。点阵列是由寻路算法提供的一组点,该算法提供以下方法中起点和终点之间的最短路径。调试完这段代码后,我知道它是有效的 我认为是CGPath给我带来了问题,它似乎没有被清除?每当我从算法中生成一条新路径时,玩家总是回到他最初开始的位置,然后他将沿着应用程序中迄今为止创建的每条路径的距离移动。每次我尝试生成新路径时都会发生这种情况 有什么想法吗 -(void)doubleTap:(UITapGestureRecognizer *)touchPoint { CGPoint t

下面的代码有点小问题。点阵列是由寻路算法提供的一组点,该算法提供以下方法中起点和终点之间的最短路径。调试完这段代码后,我知道它是有效的

我认为是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);
}
-(无效)双击:(UITapGestureRecognitor*)触点
{
CGPoint touchLocation=[touchPoint locationInView:touchPoint.view];
touchLocation=[self-convertPointFromView:touchLocation];
//双击时,获取播放机的位置和点击的位置,并将其传递给路径查找器。
CGPoint start=CGPointMake((int)(player.position.x/间距),(int)(player.position.y/间距));
CGPoint end=CGPointMake((int)(touchLocation.x/间距),(int)(touchLocation.y/间距));
NSMutableArray*points=[NSMutableArray arrayWithArray:[self reverseArray:[pathFinder findPath:start-End:End]];
//将路径转换为精灵的可移动路径,沿此路径移动精灵。
CGMutablePathRef path=CGPathCreateMutable();
如果(点数>0)
{
PathFindingNode*firstNode=[点对象索引:0];
CGPathMoveToPoint(path,NULL,firstNode.position.x,firstNode.position.y);
对于(int i=1;i
传递此代码的路径时,某个地方出现内存泄漏:

 //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);
}
//将路径转换为精灵的可移动路径,沿此路径移动精灵。
CGMutablePathRef path=CGPathCreateMutable();
如果(点数>0)
{
PathFindingNode*firstNode=[点对象索引:0];
CGPathMoveToPoint(path,NULL,firstNode.position.x,firstNode.position.y);
对于(int i=1;i

如果我把这段代码注释掉,那么iPad上的内存保持在50mb左右。如果它没有被注释掉,那么它会越来越高,直到崩溃到1.5gb左右。

您发布的代码将创建一个新路径,并用点数组中的点填充它。点阵列始终包含以前的点和新点,或者场景工具包方法followPath:asOffset:orientToPath:duration:将新路径附加到旧路径。(我还没有实际使用场景工具包,所以我不知道最后一种可能性。)


在任何情况下,您的CGPath处理代码看起来都不错。你记得要发布CGPath,而很多只知道ARC的人却不这么做。

我如何检查它是否附加了CGPath?我已经编辑了我的问题,添加了一些我在进一步调试后发现的额外信息。不知道。除了我读过的摘要和我构建并运行的演示应用程序之外,我对scene kit了解不多。希望其他对SceneKit有更多了解的人也能加入进来。这与生成的前一条路径有关,下一条路径的开始节点仍然有来自之前生成的路径的父节点,在搜索路径时将其父节点设置为零修复了此问题!