0

在我的 SpriteKit 场景中,用户应该能够用他/她的手指画线。我有一个可行的解决方案,如果线很长,FPS 下降到 4-6 并且线开始变得多边形,如下图所示:

在此处输入图像描述

为了绘制myline( ),我以这种方式SKShapeNode*收集触摸点移动NSMutableArray* noteLinePoints

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInNode:self.scene];
    SKNode *node = [self nodeAtPoint:touchPoint];

    if(noteWritingActive)
    {
        [noteLinePoints removeAllObjects];
        touchPoint = [[touches anyObject] locationInNode:_background];
        [noteLinePoints addObject:[NSValue valueWithCGPoint:touchPoint]];
        myline = (SKShapeNode*)node;
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(myline)
    {
        CGPoint touchPoint = [[touches anyObject] locationInNode:_background];
        [noteLinePoints addObject:[NSValue valueWithCGPoint:touchPoint]];
        [self drawCurrentNoteLine];
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if(myline)
    {
        myline.name = @"note";
        myline = nil;
    }
    NSLog(@"touch ended");
}

我以这种方式画线

- (CGPathRef)createPathOfCurrentNoteLine
{
    CGMutablePathRef ref = CGPathCreateMutable();

    for(int i = 0; i < [noteLinePoints count]; ++i)
    {
        CGPoint p = [noteLinePoints[i] CGPointValue];
        if(i == 0)
        {
            CGPathMoveToPoint(ref, NULL, p.x, p.y);
        }
        else
        {
            CGPathAddLineToPoint(ref, NULL, p.x, p.y);
        }
    }

    return ref;
}

- (void)drawCurrentNoteLine
{
    if(myline)
    {
        SKNode* oldLine = [self childNodeWithName:@"line"];
        if(oldLine)
            [self removeChildrenInArray:[NSArray arrayWithObject:oldLine]];

        myline = nil;

        myline = [SKShapeNode node];
        myline.name = @"line";
        [myline setStrokeColor:[SKColor grayColor]];

        CGPathRef path = [self createPathOfCurrentNoteLine];
        myline.path = path;
        CGPathRelease(path);

        [_background addChild:myline];
    }
}

我该如何解决这个问题?因为所有后续线都是多边形的,我认为是因为 fps 非常低,并且触摸的采样率自动变得非常低......

请注意,我使用 iPad 3 进行测试(我的应用程序需要在 iPad 2 型号和 iOS7 上运行)

4

1 回答 1

3

不要不断地创建新SKShapeNodes的,有一个错误导致你的速度变慢。相反,只使用 1 SKShapeNode(或创建一堆但重复使用它们),并在路径中附加新信息(因此无需不断将 myline 添加到后台)

选择:

使用 1 社区SKSkapeNode来渲染路径,然后将 转换SKShapeNode为带有 的纹理view.textureFromNode,然后使用此新纹理添加一个SKSpriteNode而不是形状节点

于 2016-08-19T13:05:15.523 回答