Question

Having a little bit of trouble with the code below. The points array is a set of points provided by a pathfinding algorithm which gives the shortest path between start and end CGPoints made in the method below. Having debugged this code I know it works.

I think its the CGPath that is causing me issues, it doesn't seem to be clearing? Whenever I generate a new path from the algorithm, the player always moves back to the position he first started in, he will then move along the distance of every path created so far within the application. This happens every time I try generate a new path.

Any ideas?

-(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);
}

There is a memory leak somewhere when passing the path to this code:

 //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);
}

If I comment this code out then the memory stays around 50mb on an iPad. If it's not commented out then it just keeps going higher and higher until it crashes around 1.5gb.

Was it helpful?

Solution

The code you posted will create a new path and populate it with the points in your array of points. Either your array of points always contains the previous points PLUS your new points, or the scene kit method followPath:asOffset:orientToPath:duration: is appending the new path to the old. (I haven't actually used scene kit yet, so I don't know about that last possibility.)

In any case, your CGPath-handling code looks good. You remembered to CGRelease the CGPath, which lots of people who only know ARC don't do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top