2014-02-11 24 views
1

对下面的代码有点麻烦。点阵列是由寻路算法提供的一组点,该算法给出下面方法中开始和结束CGPoint之间的最短路径。调试了这段代码后,我知道它有效。CGPath麻烦?

我认为它是导致我问题的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); 
} 

如果我评论此代码。随后将内存留50MB左右,在iPad上。如果它没有被注释掉,那么它会继续走得越来越高,直到它崩溃在1.5GB左右。

回答

1

您发布的代码将创建一个新路径并用点数组中的点填充它。无论是你的点数组总是包含以前的点PLUS你的新点,或场景套件方法followPath:asOffset:orientToPath:duration:将新路径追加到旧的。 (我还没有实际使用过场景套件,所以我不知道最后的可能性。)

无论如何,您的CGPath处理代码看起来不错。你记得CGRelease是CGPath,很多只知道ARC的人不会这样做。

+0

我该如何检查它是否附加它?我通过进一步调试后发现的一些额外信息编辑了我的问题。 – dev6546

+0

邓诺。除了我读过的摘要以及我已经构建和运行的演示应用程序以外,对于场景套件我都不太了解。希望有更多关于SceneKit的知识的其他人可以权衡。 –

+0

它与前面生成的路径有关,下一个路径的起始节点仍然有父路径,之前生成的路径中父路由于搜索固定路径! – dev6546