使用spritekit和UITouch绘制直线会创建多行

时间:2014-03-12 14:03:05

标签: ios objective-c sprite-kit uitouch

应该很简单。

我试图使用UITouch和Spritekit绘制一条直线。但是,当调用touchesmoved时,它会创建多行而不是一行。使用的代码如下:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    positionInScene1 = [touch locationInNode:self];

    pathToDraw = CGPathCreateMutable();

    selectorLine = [SKShapeNode node];
    selectorLine.strokeColor = [SKColor greenColor];
    selectorLine.lineWidth = 5;
    [self addChild:selectorLine];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    pathToDraw2 = CGPathCreateMutable();
    UITouch* touch = [touches anyObject];
    positionInScene2 = [touch locationInNode:self];

    CGPathMoveToPoint(pathToDraw2, NULL, positionInScene1.x, positionInScene1.y);
    CGPathAddLineToPoint(pathToDraw2, NULL, positionInScene2.x, positionInScene2.y);
    CGPathCloseSubpath(pathToDraw);

    selectorLine.path = pathToDraw;
}

如果我搬家

CGPathAddLineToPoint(pathToDraw, NULL, positionInScene2.x, positionInScene2.y);

到touchesEnd,它只会在用户结束触摸后创建一条线。我希望用户在触摸时看到正在绘制的线条。

谢谢, 道格

1 个答案:

答案 0 :(得分:3)

在touchesMoved中创建一个新路径。您正在修改相同的路径,只是添加越来越多的行。