首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGPath麻烦?

CGPath麻烦?
EN

Stack Overflow用户
提问于 2014-02-11 18:58:18
回答 1查看 269关注 0票数 1

下面的代码有点问题。点数组是由路径查找算法提供的一组点,它给出了在下面的方法中生成的起点和结束CGPoints之间的最短路径。在调试了这段代码之后,我知道它能工作。

我认为是CGPath引起了我的问题,它似乎没有清理?每当我从算法中生成一个新路径时,玩家总是回到他最初开始的位置,然后他将沿着应用程序中创建的每条路径的距离移动。每次我尝试生成一条新的路径时,都会发生这种情况。

有什么想法吗?

代码语言:javascript
复制
-(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);
}

当传递到此代码的路径时,某个地方出现了内存泄漏:

代码语言:javascript
复制
 //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左右崩溃。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-11 19:09:40

您发布的代码将创建一个新路径,并将其填充到点数组中的各个点中。无论是您的点数组总是包含前面的点加上您的新点,还是场景工具包方法跟随路径:asOffset:orientToPath:D时程:是将新路径追加到旧路径。(我还没有真正使用过现场工具包,所以我不知道最后的可能性。)

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21710684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档